gpt4 book ai didi

c# - 如何使用 NSubstitute 模拟惰性类

转载 作者:太空狗 更新时间:2023-10-29 17:48:33 24 4
gpt4 key购买 nike

//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 lazily-initialized type does not have a public, parameterless constructor

我正在使用 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);

关于c# - 如何使用 NSubstitute 模拟惰性类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8119482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com