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

上面的代码引发异常。



我正在使用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);

关于c# - 如何使用NSubstitute模拟一个懒惰的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8119482/

10-08 22:51