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
[TestInitialize]
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
ServiceCollection.AddSingleton(uowMock.Object);
BuildServiceProvider();
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();
}
UploadFilesService
void SetStatus()
{
unitOfWork.UploadFilesRepository.SetStatus(files, status);
}
Error: UploadFilesRepository is null.
I try to instantiate the repository in multiple ways:
// Mocks
var uploadFilesRepositoryMock = new Mock<UploadFilesRepositoryMock>();
uowMock = new Mock<UnitOfWork>(null, uploadFilesRepositoryMock );
// Register services
ServiceCollection.AddSingleton(uowMock.Object);
Error on ServiceCollection.AddSingleton
.
Message=Can not instantiate proxy of class: Repositories.UnitOfWork.
Could not find a constructor that would match given arguments:...
uowMock = new Mock<IUnitOfWork>(null, uploadFilesRepositoryMock);
Error:
Constructor arguments cannot be passed for interface mocks.
uow.SetupGet()
.System.ArgumentException
HResult=0x80070057
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().