[英]How to use NSubstitute to mock a lazy class
//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);
上面的代碼拋出異常。
lazily-initialized類型沒有公共的無參數構造函數
我正在使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.