Search code examples
c#.netrhino-mocks

How to Stub a ReadOnly property of a concrete class


Let's say I have a Concrete class with a Read-Only property e.g.

public class TestClass
{
   public bool Squid {get;private set;}
}

And now I want to stub the response to Squid e.g.

Squid squid = MockRepository.GenerateStub<Squid>();
squid.Stub(c => c.Squid).Return(true);

However when I run this I get the following error message: System.InvalidOperationException : Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).

Is there any way of stubbing this property without creating an interface for the class?


Solution

  • Yes, there is: Make the property virtual as already described in the exception message.