Unit of Work has null repository property in Unit Test
Asked Answered
R

1

0

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:

  1. 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:...
  1. Pass constructor arguments to interface mock.
    uowMock = new Mock<IUnitOfWork>(null, uploadFilesRepositoryMock);

Error:

Constructor arguments cannot be passed for interface mocks.
  1. 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.

Rooftree answered 21/11, 2023 at 15:43 Comment(2)
How unitOfWork is created in UploadFilesRepository class?Rae
The repository classes have no dependency on the UoW. The UoW has repositories as properties and accesses their methods to get to the db context. class UploadFilesRepository : GenericRepository<UploadFileModel>, IUploadFilesRepository. Based on the tutorial Repository PatternRooftree
R
0

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().

Rooftree answered 22/11, 2023 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.