NSubstitute Received(count) 始终为绿色(模拟具体 class 时)
NSubstitute's Received(count) is always green (when mocking concrete class)
编辑:在我进一步隔离的工作中,问题仅出现在模拟具体 class 时,而不是界面,因此我正在编辑标题。
上下文
NSubstitute 的 .Received(...) 似乎总是绿色的,无论是否发生任何调用。
为了隔离这个问题,我创建了一个两行单元测试,实际上不调用任何东西,仍然检查 got 10 calls 是绿色的:
var processor = Substitute.For<Processor>();
processor.Received(10).Process(Arg.Any<MyType>()); // this is green (do not throws)
使用最新的稳定版 3.1.0
问题
我错过了什么吗?如果是,检查特定调用次数的正确方法是什么? (坏消息是,如果这是一个错误,我们的项目充满了潜在的假果岭。
NSubstitute 仅适用于 class 上的虚拟成员。来自 Creating a substitute:
Warning: Substituting for classes can have some nasty side-effects. For starters, NSubstitute can only work with virtual members of the class, so any non-virtual code in the class will actually execute! If you try to substitute for your class that formats your hard drive in the constructor or in a non-virtual property setter then you’re asking for trouble. If possible, stick to substituting interfaces.
这意味着您只能对标记为虚拟的成员使用 .Received()
、When()..Do()
和 .Returns
。替换接口时,所有成员都可以正常工作。
编辑:在我进一步隔离的工作中,问题仅出现在模拟具体 class 时,而不是界面,因此我正在编辑标题。
上下文
NSubstitute 的 .Received(...) 似乎总是绿色的,无论是否发生任何调用。 为了隔离这个问题,我创建了一个两行单元测试,实际上不调用任何东西,仍然检查 got 10 calls 是绿色的:
var processor = Substitute.For<Processor>();
processor.Received(10).Process(Arg.Any<MyType>()); // this is green (do not throws)
使用最新的稳定版 3.1.0
问题
我错过了什么吗?如果是,检查特定调用次数的正确方法是什么? (坏消息是,如果这是一个错误,我们的项目充满了潜在的假果岭。
NSubstitute 仅适用于 class 上的虚拟成员。来自 Creating a substitute:
Warning: Substituting for classes can have some nasty side-effects. For starters, NSubstitute can only work with virtual members of the class, so any non-virtual code in the class will actually execute! If you try to substitute for your class that formats your hard drive in the constructor or in a non-virtual property setter then you’re asking for trouble. If possible, stick to substituting interfaces.
这意味着您只能对标记为虚拟的成员使用 .Received()
、When()..Do()
和 .Returns
。替换接口时,所有成员都可以正常工作。