[英]How to use NSubstitute to mock a lazy class
//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);
上面的代码抛出异常。
lazily-initialized类型没有公共的无参数构造函数
我正在使用C#4.0和NSubstitute 1.2.1
根据@ sanosdole的评论,我建议使用一个真正的Lazy
实例来返回你的替代品。 就像是:
var notificationService = Substitute.For<INotificationService>();
var target = new Service(repository, new Lazy<INotificationService>(() => notificationService));
target.SendNotify("Message");
notificationService.ReceivedWithAnyArgs().sendNotification(null, null, null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.