C# 如何使用NSubstitute模拟懒惰类 //断言 Lazy notificationService=替换.For(); 服务目标=新服务(存储库、通知服务); //表演 发送通知(“消息”); //安排 notificationService.Received().Value.sendNotification(null,null,null,null);
上面的代码抛出一个异常 延迟初始化的类型没有公共的无参数构造函数C# 如何使用NSubstitute模拟懒惰类 //断言 Lazy notificationService=替换.For(); 服务目标=新服务(存储库、通知服务); //表演 发送通知(“消息”); //安排 notificationService.Received().Value.sendNotification(null,null,null,null);,c#,unit-testing,mocking,lazy-evaluation,nsubstitute,C#,Unit Testing,Mocking,Lazy Evaluation,Nsubstitute,上面的代码抛出一个异常 延迟初始化的类型没有公共的无参数构造函数 我正在使用C#4.0和NSubstitute 1.2.1根据@sanosdole的评论,我建议使用一个真正的Lazy实例来返回您的替代品。比如: //Assert Lazy<INotificationService> notificationService = Substitute.For<Lazy<INotificationService>>(); Service target = new S
我正在使用C#4.0和NSubstitute 1.2.1根据@sanosdole的评论,我建议使用一个真正的
Lazy
实例来返回您的替代品。比如:
//Assert
Lazy<INotificationService> notificationService = Substitute.For<Lazy<INotificationService>>();
Service target = new Service(repository, notificationService);
//Act
target.SendNotify("Message");
//Arrange
notificationService.Received().Value.sendNotification(null, null, null, null);
var notificationService=Substitute.For();
var target=new服务(存储库,new Lazy(()=>notificationService));
发送通知(“消息”);
notificationService.ReceivedWithAnyArgs().sendNotification(null,null,null,null);
你真的想替换懒惰的人吗?我假设Lazy可以工作,并使用它的值工厂构造函数,为@sanosdole的注释提供replacement.For()作为值工厂…+1。我已经在社区维基上发布了这个答案。宾果游戏-与上面的sanosdole评论一起对我有效
var notificationService = Substitute.For<INotificationService>();
var target = new Service(repository, new Lazy<INotificationService>(() => notificationService));
target.SendNotify("Message");
notificationService.ReceivedWithAnyArgs().sendNotification(null, null, null, null);