我正在尝试模拟对象的一个属性
有一个类似的问题: 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);
最佳答案
好的,这有点棘手。首先,解决方案是阻止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
one取代,并且Returns
可以按预期工作。诚然,这非常糟糕,这也是我们长期以来一直将部分模拟放到NSubstitute中的原因之一。 NSub的语法不错,但代价是它并不总是能够区分开发人员是在配置调用时还是在实际代码需要运行时。对困惑感到抱歉。