13

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

当我运行上述测试时,出现以下异常:

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 an interface, check that the call to your substitute was on a virtual/abstract member.
Return values cannot be configured for non-virtual/non-abstract members.

但是,如果我更改测试方法以返回:

sub.MyProperty.Returns((a) => MyMethod());

或这个:

var result = MyMethod();
sub.MyProperty.Returns(result);

有用。

我只是想知道是否有人可以解释为什么会发生这种情况?

4

1 回答 1

24

为了让 NSubstitute 语法正常工作,幕后发生了一些混乱。这是它咬我们的案例之一。让我们先看一下您的示例的修改版本:

sub.MyProperty.Returns(someValue);

首先,sub.MyProperty被调用,它返回一个IMyObject. 然后Returns调用扩展方法,它需要以某种方式确定它需要返回哪个调用someValue。为此,NSubstitute 会在某个全局状态下记录它收到的最后一次调用。Returns在伪 ish 代码中看起来像这样:

public static void Returns<T>(this T t, T valueToReturn) {
  var lastSubstitute = bigGlobOfStaticState.GetLastSubstituteCalled();
  lastSubstitute.SetReturnValueForLastCall(valueToReturn);
  bigGlobOfStaticState.ClearLastCall(); // have handled last call now, clear static state
}

所以评估整个调用看起来有点像这样:

sub.MyProperty         // <-- last call is sub.MyProperty
   .Returns(someValue) // <-- make sub.MyProperty return someValue and
                       //     clear last call, as we have already set
                       //     a result for it

现在让我们看看当我们在尝试设置返回值时调用另一个替代品时会发生什么:

sub.MyProperty.Returns(MyMethod());

再这样评价sub.MyProperty,便需要评价Returns。在它可以这样做之前,它需要评估 的参数Returns,这意味着运行MyMethod()。这个评估看起来更像这样:

//Evaluated as:
sub.MyProperty     // <- last call is to sub.MyProperty, as before
   .Returns(
     // Now evaluate arguments to Returns:
     MyMethod()
       var ob = Substitute.For<IMyObject>()
       ob.Value      // <- last call is now to ob.Value, not sub.MyProperty!
         .Returns(1) // <- ok, ob.Value now returns 1, and we have used up the last call
     //Now finish evaluating origin Returns:
     GetLastSubstituteCalled *ugh, can't find one, crash!*

这里还有另一个可能导致问题的例子。

您可以通过使用以下方法将调用推迟到 来解决此问题MyMethod()

sub.MyProperty.Returns(x => MyMethod());

这是有效的,因为MyMethod()它只会在需要使用返回值时执行,所以静态GetLastSubstituteCalled方法不会混淆。

不过,与其这样做,我更愿意在忙于配置一个替代品时避免其他对替代品的调用。

希望这可以帮助。:)

于 2013-05-20T10:10:07.643 回答