mocking classes and virtual methods

466 views
Skip to first unread message

Brian Chan

unread,
Jun 5, 2007, 10:05:24 PM6/5/07
to Rhino.Mocks
I haven't read it anywhere, but I assume methods must be virtual in
order to mock a class.

Is it possible to throw an exception if trying to call
Expect.Call(...).. on a non-virtual method?

Ayende Rahien

unread,
Jun 6, 2007, 12:26:57 AM6/6/07
to Rhino...@googlegroups.com
Not really, an exception will be raised in this case, however:

Expect.Call( mocked.NonVirtualMethod() ).Return(null);

Brian Chan

unread,
Jun 6, 2007, 8:50:34 PM6/6/07
to Rhino.Mocks
So the exception raised when trying to mock a non-virtual method is:

Invalid call, the last call has been used or no call has been made.
System.InvalidOperationException: Invalid call, the last call has been
used or no call has been made.
at Rhino.Mocks.LastCall.get_Options()
at Rhino.Mocks.Expect.Call(Object ignored)...

I don't think this is very descriptive of the fact the the method was
not marked virtual. Is it possible to return a better exception
message in this case?

Stefan Podskubka

unread,
Jun 6, 2007, 10:24:25 PM6/6/07
to Rhino.Mocks
Brian,

the documentation page you were looking for is here:
http://www.ayende.com/Wiki/(S(eqbbidusglkuv145m4s4fq3j))/Rhino+Mocks+Limitations.ashx

I will try to explain the problem to you.
As far as I understand it, what you want is not possible because Rhino
Mocks cannot distinguish between calling a non-virtual method and
calling nothing at all.

Let's say you have the following class:

class BaseClass
{
public virtual int Method() { return 0; }
}

When you mock this class, RhinoMocks actually implements a class that
is derived from BaseClass and overrides all virtual methods with its
own implementation which essentially notifies the mocking repository
about the call and you can access this call for example by writing
LastCall.Return(1).

So, if Method() was non-virtual, RhinoMocks would not be able to
override this method with it's own implementation and thus the mocking
repository would not be notified about a call to Method().

Did that answer your question?

> > > Expect.Call(...).. on a non-virtual method?- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Brian Chan

unread,
Jun 6, 2007, 11:24:03 PM6/6/07
to Rhino...@googlegroups.com
Thanks for the explanation. It makes it clearer now.

Brian.

Shane C

unread,
Jun 7, 2007, 10:30:42 AM6/7/07
to Rhino.Mocks
I know it's a slipperly slope but I've seen error messages that
sometimes give you a small pointer as to the possible problem.
Especially in a situation like this where a large percentage of the
time it's people calling a non-virtual method. Something like the
following:

Invalid call, the last call has been used or no call has been made.
System.InvalidOperationException: Invalid call, the last call has
been

used or no call has been made. (Did you call a non-virtual method?)

Shane

On Jun 6, 9:24 pm, "Brian Chan" <bch...@gmail.com> wrote:
> Thanks for the explanation. It makes it clearer now.
>
> Brian.
>

> On 6/6/07, Stefan Podskubka <s.podsku...@hcs.at> wrote:
>
>
>
>
>
> > Brian,
>
> > the documentation page you were looking for is here:

> >http://www.ayende.com/Wiki/(S(eqbbidusglkuv145m4s4fq3j))/Rhino+Mocks+...

> > > - Zitierten Text anzeigen -- Hide quoted text -
>
> - Show quoted text -

Ayende Rahien

unread,
Jun 8, 2007, 1:03:10 PM6/8/07
to Rhino...@googlegroups.com
Applied, thanks
Reply all
Reply to author
Forward
0 new messages