为测试中的类提供必要的依赖关系,以使测试能够完成.//Arrangevar 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);//ActcarService.Work();//AssertauditRepositoryMock.Verify(_ => _.Insert(It.IsAny<string>()), Times.Once);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 classpublic 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(); 解决方案 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.//Arrangevar 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);//ActcarService.Work();//AssertauditRepositoryMock.Verify(_ => _.Insert(It.IsAny<string>()), Times.Once); 这篇关于如何对依赖于基类的方法进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 10:51