2011-11-14 9 views
8
//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); 

上記のコードは例外をスローします。NSubstituteを使用して遅延クラスをモックする方法

いい加減に初期化タイプは、私が使用することをお勧めします、@ sanosdoleさんのコメントを1として、私はC#4.0とを使用しています公共、パラメータなしのコンストラクタ

NSubstitute 1.2.1

+1

本当にLazyを代用したいのですか?私はちょうどLazy <>がValue Factoryのコンストラクタを使用し、Substitute.For ()をValue Factoryとして使用していると仮定します。 – sanosdole

+0

+1 to @ sanosdoleのコメント。その答えをコミュニティのwikiとして投稿しました。 –

答えて

9

を持っていません代理店を返すには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); 
関連する問題