NSubstitute CouldNotSetReturnDueToNoLastCallException

and*_*fox 6 nsubstitute

我正在使用NSubstitute来通过PartsOf()方法来模拟一个类(我需要一些方法来工作).它看起来像这样:

var mock = Substitute.ForPartsOf<MyWorker>();
mock.Start().Returns(void);
Run Code Online (Sandbox Code Playgroud)

一个简单的代码几乎与NSubstitute的文档相似,但我得到了这个例外: "An exception of type 'NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException' occurred in NSubstitute.dll but was not handled in user code Additional information: Could not find a call to return from."

有什么问题?

and*_*fox 9

我花了一些时间来弄明白.使用ForPartsOf()模拟方法时一定要virtual!这解决了CouldNotSetReturnDueToNoLastCallException异常的问题.