我在使用NSubstitute几次时遇到了一个奇怪的问题,虽然我知道如何解决它,但我从来没有能够解释它.
我已经精心设计了证明问题的最低要求测试,它似乎与使用方法创建替代返回值有关.
public interface IMyObject
{
int Value { get; }
}
public interface IMyInterface
{
IMyObject MyProperty { get; }
}
[TestMethod]
public void NSubstitute_ReturnsFromMethod_Test()
{
var sub = Substitute.For<IMyInterface>();
sub.MyProperty.Returns(MyMethod());
}
private IMyObject MyMethod()
{
var ob = Substitute.For<IMyObject>();
ob.Value.Returns(1);
return ob;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的测试时,我得到以下异常:
Test method globalroam.Model.NEM.Test.ViewModel.DelayedAction_Test.NSubstitute_ReturnsFromMethod_Test threw exception:
NSubstitute.Exceptions.CouldNotSetReturnException: Could not find a call to return from.
Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)).
If you substituted for a class rather than …Run Code Online (Sandbox Code Playgroud)