Stubbing Partial Mocks

4 views
Skip to first unread message

Vyas

unread,
Aug 11, 2008, 6:22:24 PM8/11/08
to Rhino.Mocks
public class EventManager
{
public virutal void SomeMethod()
{
throws new NotImplementedException();
}
}

The partial mock with SomeMethod() stubbed:

EventManager partialMock = new
MockRepository().PartialMock<EventManager>();
partialMock.Stub(x => x.SomeMethod()).Do((Action)
delegate
{
Console.WriteLine("SomeMethod called");
partialMock.Added = DateTime.Now;
}
);
partialMock.Replay();
partialMock.SomeMethod();

Now, the above code works fine calls the stub when SomeMethod() is
called.
The moment I remove 'virtual', the original method is called. Why is
there a restriction that I can ONLY stub 'virtual' methods? What when
I do not have control over the class mocked?

[1. I'm advocating Rhino Mocks in my group for mocking (stub, I
mean :)) pieces under parallel development.
2. They could stub methods they need and the return values they expect
3. Use DI to inject the stub into real code from the Unit Tests]

The inability to Stub non-virtual methods gets in the way. Any
thoughts?


Regards, Vyas

PS: The below doesn't work as well:

EventManager partialStub =
MockRepository.GenerateStub<EventManager>();
partialStub.Stub(x => x.SomeMethod()).Do((Action)
delegate
{
Console.WriteLine("SomeMethod called");
partialStub.Added = DateTime.Now;
}
);
partialStub.Replay();
partialStub.SomeMethod();

Ayende Rahien

unread,
Aug 12, 2008, 6:24:14 PM8/12/08
to Rhino...@googlegroups.com
Please read the FAQ for the reasons.

Vyas

unread,
Aug 13, 2008, 3:26:03 AM8/13/08
to Rhino.Mocks
Hm... I did find out it's Castle DynamicProxy's limitation (and
CLR's)...
I was wondering if there is a possibility of doing:

//Pseudo Code
IEventManager manager =
MockRepository.GenerateStub<IEventManager>(new EventManager()); //
Supply original object
manager.Stub(x=>x.SomeMethod()).Return(delegate() { //Do something
here }); //Stub the method that throws expection

Right now, I suspect, I need to mock the interface and stub all the
methods and route the calls to the original object manually.
This could be cumbersome if the class being stubbed has too many
methods/properties.

Regards, Vyas
> >            partialStub.SomeMethod();- Hide quoted text -
>
> - Show quoted text -

Ayende Rahien

unread,
Aug 13, 2008, 10:01:08 AM8/13/08
to Rhino...@googlegroups.com
Feel free to provide a patch to add this capability

Vyas

unread,
Aug 13, 2008, 11:59:46 AM8/13/08
to Rhino.Mocks
Absolutely.... Will try this weekend...
> > > - Show quoted text -- Hide quoted text -

Vyas

unread,
Aug 14, 2008, 5:01:15 AM8/14/08
to Rhino.Mocks
Here's the complete approach outlined...
http://www.thinkfarahead.com/2008/08/mocking-anomalies-can-override-non.html
Hope to get started on the patch later this week...

Regards, Vyas

On Aug 13, 7:01 am, "Ayende Rahien" <aye...@ayende.com> wrote:
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages