上面的代码会引发异常。
懒惰初始化类型没有公共的无参数构造函数
我正在使用C#4.0和NSubstitute 1.2.1
//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 above code throws an exception.
The lazily-initialized type does not have a public, parameterless constructor
I am using C# 4.0 and 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);As per @sanosdole's comment, I would suggest using a real Lazy instance to return your substitute. Something like:
var notificationService = Substitute.For<INotificationService>(); var target = new Service(repository, new Lazy<INotificationService>(() => notificationService)); target.SendNotify("Message"); notificationService.ReceivedWithAnyArgs().sendNotification(null, null, null, null);如何使用NSubstitute来模拟一个懒惰的课程(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);上面的代码会引发异常。
懒惰初始化类型没有公共的无参数构造函数
我正在使用C#4.0和NSubstitute 1.2.1
//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 above code throws an exception.
The lazily-initialized type does not have a public, parameterless constructor
I am using C# 4.0 and 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);As per @sanosdole's comment, I would suggest using a real Lazy instance to return your substitute. Something like:
var notificationService = Substitute.For<INotificationService>(); var target = new Service(repository, new Lazy<INotificationService>(() => notificationService)); target.SendNotify("Message"); notificationService.ReceivedWithAnyArgs().sendNotification(null, null, null, null);
发布评论