Ok, this is a bit of a tricky one. First, the solution is to stop obj.PropClass
from calling the base implementation:
obj.When(x => { var get = x.PropClass; }).DoNotCallBase();
obj.PropClass.Returns(prop);
Now the explanation. NSubstitute records the calls made on a substitute, and when we call Returns
, it grabs the last call made and tries to configure that to return a specific value.
What's happening when obj.PropClass.Returns(prop)
runs is that the real obj.PropClass
is called, which in turn calls obj.PropClasses
, so NSubstitute now thinks obj.PropClasses
is the last call. The Returns
then tries to return a single PropClass
when PropClasses
expects an IEnumerable<PropClass>
, hence the exception.
The fix above stops obj.PropClass
from calling the base implementation immediately, so the last call does not get replaced with the PropClasses
one and Returns
can work as expected.
Admittedly this is quite awful, and one of the reasons we held out putting partial mocks into NSubstitute for so long. NSub's nice syntax comes at the price of it not always being able to distinguish when the developer is configuring a call from when real code needs to run. Sorry for the confusion.