Unit of Work has null repository property in Unit Test

I cannot run a unit test from the BLL/Service layer. The test mocks the Unit of Work because its a unit test for a service method. Thus, the uow mock has a null repository property which fails the unit test.

Unit Test Initialize

public void TestInitialize()
  ServiceCollection.AddSingleton<IUploadFilesService, UploadFilesService>();

  // Mocks
  var uploadFilesRepositoryMock = new Mock<UploadFilesRepository>();
  uowMock = new Mock<IUnitOfWork>();
  uowMock.SetupGet(el => el.UploadFilesRepository).Returns(uploadFilesRepositoryMock.Object); 
  // errors here because interface has no setter

  // Register services

  service = (UploadFilesService)ServiceProvider.GetService<IUploadFilesService>();

The interface has only getters for safety and probably should remain that way.

public interface IUnitOfWork : IDisposable
    IUploadFilesRepository UploadFilesRepository { get; }
    int Complete();


void SetStatus() 
    unitOfWork.UploadFilesRepository.SetStatus(files, status);

Error: UploadFilesRepository is null.

I try to instantiate the repository in multiple ways:

  1. Mock concrete class and not interface.
// Mocks
var uploadFilesRepositoryMock = new Mock<UploadFilesRepositoryMock>();
uowMock = new Mock<UnitOfWork>(null, uploadFilesRepositoryMock );

// Register services

Error on ServiceCollection.AddSingleton.

Message=Can not instantiate proxy of class: Repositories.UnitOfWork.
Could not find a constructor that would match given arguments:...
  1. Pass constructor arguments to interface mock.
    uowMock = new Mock<IUnitOfWork>(null, uploadFilesRepositoryMock);


Constructor arguments cannot be passed for interface mocks.
  1. Use uow.SetupGet().
  Message=Can not instantiate proxy of class: 
Could not find a parameterless constructor. (Parameter 'constructorArguments')

I searched other questions regarding these unit test errors, but they don't tackle the case in which I use dependecy injection AddSingleton method. Which for consistency I use in all my tests in [TestInitialize] for a clearer code format.


  • It seems I can setup the UoW mock and the getter method by using only Setup and it works this way.

    var uploadFilesQueueRepositoryMock = new Mock<IUploadFilesQueueRepository>();
    uowMock = new Mock<IUnitOfWork>();
    uowMock.Setup(m => m.UploadFilesQueueRepository).Returns(uploadFilesQueueRepositoryMock.Object);

    I tried SetupGet(), mock concrete class, AddSingleton, but the solution was only using the method Setup().