How do I unit test the method which has dependency on base classes?

荒凉一梦 提交于 2019-12-24 19:54:21

问题


I have a below class, which having base class and I am trying to write unit test.

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 is part of base class. CurrentContext.ContainerFactory throws null exception. How do I create Mock for these classes?

Is interface is must for unit testing?

Updated with base class

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 class

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

Unit Test Class

       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:


You did not setup the container factory's behavior so when you call .GetInstance<IAuditRepository>() it will return null, hence your error.

Provide the class under test with the necessary dependencies to allow the test to be exercised to completion.

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


来源:https://stackoverflow.com/questions/51473303/how-do-i-unit-test-the-method-which-has-dependency-on-base-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!