Mocking Classes in Rhino.Mocks

7 views
Skip to first unread message

Andyk

unread,
Nov 12, 2008, 7:28:16 AM11/12/08
to ALTNET
Hi all,
Can anyone help me with this? Im trying to figure out if I can mock a
concrete implementation? Or do I always have to mock against
interfaces.

I have a DataType which contains a Property to do some calculation.
This is based on a value return from a property from the
ParentCollection.
Im trying to unit test the DataType calculation, and wanting to mock
the ParentCollection property.
But it seemingly wont let me, unless I introduce an interface first.
Im trying to unit test legacy code, so breaking everything up and
introducing interfaces for all the datatypes is going to be alot of
work.

If I try to do something like:

ClientCollection mockCollection = mockery.StrictMock<ClientCollection>
();

When I call:
Expect.Call(mockCollection.TotalShares).return(100);

I get the following error:

Test method RhinoTestProject.ClientTest.ShouldTestMarketShares threw
exception: 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)..

And when I actually step through the test, its trying to call the
actual implementation rather than just return my mock value.


So is what Im trying to do possible?


Kinds regards


Andy

Andyk

unread,
Nov 12, 2008, 7:39:36 AM11/12/08
to ALTNET
I should add to this that I had looked at the Rhino.Mocks wiki, and
followed the examples. However it seems to be out of date as it
recommends CreateMock<T>. But the latest version says that CreateMock
is obselete and to use StrictMock.

Ayende Rahien

unread,
Nov 12, 2008, 9:20:46 AM11/12/08
to alt...@googlegroups.com
Is the method that you are trying to mock virtual?
Reply all
Reply to author
Forward
0 new messages