如何对依赖于基类的方法进行单元测试?

时间:2018-07-23 07:09:24

标签: c# unit-testing moq

我有一个以下类,它具有基类,并且我正在尝试编写单元测试。

public class CarService : ServiceBase, IProvisioningService
{
    private IAuditRepository _repository;
    public CarService(IHostingFactory hostingFactory) : base(name, hostingFactory)
    {         

    }

    public override void DoWork()
    {
        if (_repository == null)
          {
             //its calling the base method.
            _repository = CurrentContext.ContainerFactory.GetInstance<IAuditRepository>();                  
        try
        {
            _repository.Insert("something");
        }
        catch (Exception ex)
        {

        }
    }       
  }
}

CurrentContext.ContainerFactory是基类的一部分。 CurrentContext.ContainerFactory引发空异常。如何为这些课程创建Mock?

对单元测试来说,接口是必须的吗?

已更新基类

public abstract class ServiceBase : IServiceBase
{
    public HostingContext CurrentContext { get; }
    public string ServiceName { get; }

    protected ServiceBase(string serviceName, IHostingFactory hostingFactory)
    {
        ServiceName = serviceName;

        _stopSignal = false;
        CurrentContext = hostingFactory.CreateContext(serviceName);
        Logger = CurrentContext.LoggerInstance;
    }

}

HostingContext类

   public class HostingContext
   {
        public HostingContext(
        Func<string, ILogger> loggerFactory,
        string serviceName, 
        string connString): this(loggerFactory(contextName),serviceName, connString, new ContainerFactory())
    {}
 }

单元测试课程

       MockRepository repository = new MockRepository(MockBehavior.Default);
       var containerFactoryMock = repository.Create<IContainerFactory>();
       var auditRepositoryMock = repository.Create<IAuditRepository>();
       var hostingFactoryMock = repository.Create<IHostingFactory>();
                   var hostingContextMock = new HostingContext("Sample", "ConnString",containerFactoryMock.Object);


        hostingFactoryMock.Setup(factory => factory.CurrentContext(It.IsAny<string>()))
            .Returns(hostingContextMock);
        CarService carService = new CarService(hostingFactoryMock.Object);

        carService.Work();

1 个答案:

答案 0 :(得分:1)

您没有设置容器工厂的行为,因此,当您调用.GetInstance<IAuditRepository>()时,它将返回 null ,因此会出错。

为测试中的类提供必要的依赖关系,以使测试能够完成。

//Arrange
var repository = new MockRepository(MockBehavior.Default);
var containerFactoryMock = repository.Create<IContainerFactory>();
var auditRepositoryMock = repository.Create<IAuditRepository>();
var hostingFactoryMock = repository.Create<IHostingFactory>();
var loggerMock = repository.Create<ILogger>();

var hostingContextMock = new HostingContext(loggerMock, "Sample", "ConnString",containerFactoryMock.Object);

hostingFactoryMock
    .Setup(_ => _.CreateContext(It.IsAny<string>()))
    .Returns(hostingContextMock);

containerFactoryMock
    .Setup(_ => _.GetInstance<IAuditRepository>())
    .Returns(auditRepositoryMock);

CarService carService = new CarService(hostingFactoryMock.Object);

//Act
carService.Work();

//Assert
auditRepositoryMock.Verify(_ => _.Insert(It.IsAny<string>()), Times.Once);