I'm using Rhino.Mocks 3.6 for the first time. I'm trying to create a stub for an interface that returns an inherited type (B). When I try to do this, it will generate an InvalidCastException
trying to convert some proxy object to the base class (A).
For example:
class A {}
class B : A {}
interface IMyInterface
{
A GetA();
}
// Create a stub
var mocks = new MockRepository();
var stub = mocks.Stub<IMyInterface>();
Expect.Call( stub.GetA() ).Return( new B() );
// This will throw an InvalidCastException
var myA = stub.GetA();
It seems to me that the problem is that it's generating proxy classes that do not have the same inheritance structure as the existing classes. However, it seems to me like a fairly common situation to return a subclass of the type specified by the method signature.
I've tried a few variations, but I can't get this to work. Any ideas?