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とNSubstitute1.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 に答える