11
//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

4

1 回答 1

12

根据@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);
于 2011-11-14T20:36:08.670 回答