Search code examples
c#unit-testingasp.net-corenunitunit-of-work

How can I unit test my UnitOfWork class with NUnit?


This is my UnitOfWork class :

public class UnitOfWork : IUnitOfWork
    {
        protected SampleProjectDbContext _db { get; private set; }
        private IServiceProvider _serviceProvider;
        private bool _disposed;

        public UnitOfWork(SampleProjectDbContext db, IServiceProvider serviceProvider)
        {
            _db = db;
            _serviceProvider = serviceProvider;
        }

        // some code

        public IStudentRepository StudentRepository => _serviceProvider.GetRequiredService<IStudentRepository>();
        public ICourseRepository CourseRepository => _serviceProvider.GetService<ICourseRepository>();
        public IRegisteredCourseRepository RegisteredCourseRepository => _serviceProvider.GetService<IRegisteredCourseRepository>();
    }

Now how can I write unit tests for StudentRepository property with NUnit? I can't find a way to test StudentRepository property.

This is my ServiceModuleExtentions class :

public static class ServiceModuleExtentions
    {
        public static void RegisterInfrastructureServices(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddScoped<IUnitOfWork, UnitOfWork>();
            serviceCollection.AddScoped<ICourseRepository, CourseRepository>();
            serviceCollection.AddScoped<IStudentRepository, StudentRepository>();
            serviceCollection.AddScoped<IRegisteredCourseRepository, RegisteredCourseRepository>();
        }
    }

and I use it like this in my Startup:

    public void ConfigureServices(IServiceCollection services)
            {
                // some code

                services.RegisterInfrastructureServices();
            }

I test it like this but it gives error :

public class UnitOfWork_UnitTest
    {
        private UnitOfWork _unitOfWork;
        private Mock<IServiceProvider> _serviceProviderMock;
        private Mock<SampleProjectDbContext> _dbContextMock;

        [SetUp]
        public void Setup()
        {
            _serviceProviderMock = new Mock<IServiceProvider>();
            _dbContextMock = new Mock<SampleProjectDbContext>();
            _unitOfWork = new UnitOfWork(_dbContextMock.Object, _serviceProviderMock.Object);
        }

        [Test]
        public void Should_Return_IStudentRepository()
        {
            // Arrange
            _serviceProviderMock
                .Setup(x => x.GetService(typeof(IStudentRepository)))
                .Returns(It.IsAny<IStudentRepository>());

            // Act
            var result = _unitOfWork.StudentRepository;

            // Assert
            Assert.IsAssignableFrom<IStudentRepository>(result);
        }
    }

and the error is "System.InvalidOperationException : No service for type 'SampleProject.Core.Contracts.IStudentRepository' has been registered."


Solution

  • Change your Setup method to this:

    _serviceProviderMock = new Mock<IServiceProvider>();
    _serviceProviderMock
        .Setup(x => x.GetService(typeof(IStudentRepository)))
        .Returns(new StudentRepository());
    _dbContextMock = new Mock<SampleProjectDbContext>();
    _unitOfWork = new UnitOfWork(_dbContextMock.Object, _serviceProviderMock.Object);
    

    The problem is you're currently injecting the _serviceProviderMock into the _unitOfWork constructor before you've set it up. You also need to provide an instance instead of a matcher.