使用ForPartsOf的NSubstitute虚拟getter返回替换会抛出异常

时间:2014-01-22 21:30:37

标签: c# mocking nsubstitute

我正在尝试模拟对象的一个​​属性

有一个类似的问题: Returning the result of a method that returns another substitute throws an exception in NSubstitute 但接受的答案对我不起作用。

void Main()
{
    var obj = Substitute.ForPartsOf<MyObject>();
    //WORKS, But I need a partial mock!:
    //var obj = Substitute.For<MyObject>();

    obj.PropClass.Returns(Substitute.For<PropClass>());

    //It's suggestion, Fails, same error:
    //var returnValue = Substitute.For<PropClass>();
    //obj.PropClass.Returns(returnValue);

    //Fails, same error:
    //Lazy implementation of *similar question*
    //Func<PropClass> hello = () => Substitute.For<PropClass>();
    //obj.PropClass.Returns(x => hello());

    //Fails, same error:
    //I believe what *similar question* suggests
    //obj.PropClass.Returns(x => BuildSub());

    obj.PropClass.Dump("Value");
}

public class MyObject
{
    public MyObject()
    {
        _propClasses = new List<PropClass>();
    }
    private readonly IList<PropClass> _propClasses;
    public virtual IEnumerable<PropClass> PropClasses { get { return _propClasses; } }
    public virtual PropClass PropClass { get { return PropClasses.FirstOrDefault(); } }
}
public class PropClass
{
}

public PropClass BuildSub()
{
    return Substitute.For<PropClass>();
}

这些失败,但有例外:

CouldNotSetReturnDueToTypeMismatchException:
Can not return value of type PropClassProxy_9 for MyObject.get_PropClasses (expected type IEnumerable`1).

Make sure you called Returns() after calling your substitute (for example: mySub.SomeMethod().Returns(value)),
and that you are not configuring other substitutes within Returns() (for example, avoid this: mySub.SomeMethod().Returns(ConfigOtherSub())).

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.

Correct use:
  mySub.SomeMethod().Returns(returnValue);

Potentially problematic use:
  mySub.SomeMethod().Returns(ConfigOtherSub());
Instead try:
  var returnValue = ConfigOtherSub();
  mySub.SomeMethod().Returns(returnValue);

1 个答案:

答案 0 :(得分:8)

好的,这有点棘手。首先,解决方案是阻止obj.PropClass调用基本实现:

obj.When(x => { var get = x.PropClass; }).DoNotCallBase();
obj.PropClass.Returns(prop);

现在解释。 NSubstitute记录对替换的调用,当我们调用Returns时,它会抓取最后一次调用并尝试配置它以返回特定值。

obj.PropClass.Returns(prop)运行时发生的事情是调用真实obj.PropClass,然后调用obj.PropClasses,因此NSubstitute现在认为obj.PropClasses是最后一次调用。 Returns会在PropClass期望PropClasses时尝试返回单个IEnumerable<PropClass>,因此会出现异常。

上述修复程序会立即停止obj.PropClass调用基本实现,因此最后一次调用不会被PropClasses替换,Returns可以按预期工作。

不可否认,这是非常糟糕的,也是我们坚持将部分嘲讽放入NSubstitute很长时间的原因之一。 NSub的漂亮语法是以它无法始终能够区分开发人员何时需要运行实际代码来配置呼叫为代价的。对不起,感到困惑。