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:
- Mock concrete class and not interface.
// 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:...
- Pass constructor arguments to interface mock.
uowMock = new Mock<IUnitOfWork>(null, uploadFilesRepositoryMock);
Error:
Constructor arguments cannot be passed for interface mocks.
- Use
uow.SetupGet()
.
Error:
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.
unitOfWork
is created in UploadFilesRepository class? – Raeclass UploadFilesRepository : GenericRepository<UploadFileModel>, IUploadFilesRepository
. Based on the tutorial Repository Pattern – Rooftree