//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);
以上代码抛出异常。
The lazily-initialized type does not have a public, parameterless constructor
我正在使用 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/