【问题标题】:How can I unit test my UnitOfWork class with NUnit?如何使用 NUnit 对我的 UnitOfWork 类进行单元测试?
【发布时间】:2020-04-28 17:06:17
【问题描述】:

这是我的 UnitOfWork 课程:

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>();
    }

现在如何使用 NUnit 为 StudentRepository 属性编写单元测试?我找不到测试 StudentRepository 属性的方法。

这是我的 ServiceModuleExtentions 类:

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>();
        }
    }

我在 Startup 中这样使用它:

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

                services.RegisterInfrastructureServices();
            }

我这样测试它,但它给出了错误:

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);
        }
    }

错误是“System.InvalidOperationException : No service for type 'SampleProject.Core.Contracts.IStudentRepository' has been registered。”

【问题讨论】:

  • 我没有看到该类中需要测试的任何内容。没有逻辑,只是封装了一堆属性。

标签: c# unit-testing asp.net-core nunit unit-of-work


【解决方案1】:

将您的 Setup 方法更改为:

_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);

问题是您在设置之前将_serviceProviderMock 注入到_unitOfWork 构造函数中。您还需要提供一个实例而不是匹配器。

【讨论】:

  • 我把它改成了:_serviceProviderMock = new Mock&lt;IServiceProvider&gt;(); _dbContextMock = new Mock&lt;SampleProjectDbContext&gt;(); _serviceProviderMock .Setup(x =&gt; x.GetService(typeof(IStudentRepository))) .Returns(new StudentRepository(_dbContextMock.Object)); _unitOfWork = new UnitOfWork(_dbContextMock.Object, _serviceProviderMock.Object); 它工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 2010-09-15
  • 1970-01-01
  • 2011-02-20
  • 2019-12-31
  • 2018-03-04
相关资源
最近更新 更多