Difference between MockRepository.GenerateMock<T> and MockRepository.GenerateStub<T>

1,960 views
Skip to first unread message

changhong

unread,
Nov 26, 2009, 3:53:08 PM11/26/09
to Rhino.Mocks
I use AAA syntax, and I couldn't find any difference between objects
created by MockRepository.GenerateMock<T> or
MockRepository.GenerateStub<T>.

I noticed with both objects, you can setup stubs or expectations using
obj.Stub(..) or obj.Expect(...), for both objects, test won't fail if
an expectation does not happen. Also with both objects, you can verify
an expectation using obj.AssertWasCalled(...), and they behave the
same.

Can anyone tell me what the difference between those two? And how
should I choose one over another?

Manny Thanks.

Tim Barcz

unread,
Nov 26, 2009, 10:17:05 PM11/26/09
to rhino...@googlegroups.com
There are subtle differences (most of which escape me at the moment).  However you are correct that the notion of a stub and mock as they relate to RhinoMocks has converged over the years.  In the next version of RhinoMocks we're planning to explore the notion of making the two concepts one or at the very least making the distinctions more clear.

Tim


--

You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To post to this group, send email to rhino...@googlegroups.com.
To unsubscribe from this group, send email to rhinomocks+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rhinomocks?hl=en.





--
Tim Barcz
Microsoft C# MVP
Microsoft ASPInsider
http://timbarcz.devlicio.us
http://www.twitter.com/timbarcz

David Tchepak

unread,
Nov 27, 2009, 4:00:17 AM11/27/09
to rhino...@googlegroups.com
From memory I think the main difference is that GenerateStub<T>
returns an object with normal property behaviour.

For example (psuedo-code only, haven't tested):

var stub = MockRepository.GenerateStub<ISomething>();
stub.MyProperty = 2;
Assert.AreEqual(stub.MyProperty, 2);

If you try the same with a mock then MyProperty would return 0 (or
null for reference type). If you want the property of a mock to return
a specific value you have to stub it (confusing, right? :-)).

var mock = MockRepository.GenerateMock<ISomething>();
mock.MyProperty = 2;
Assert.AreEqual(mock.MyProperty, 0);
mock.Stub(x => x.MyProperty).Return(2);
Assert.AreEqual(mock.MyProperty, 2);

I think that's right, but you should probably run it and make sure I
have a vague idea of what I'm talking about. ;)

My approach is to use GenerateStub<T> everywhere. I don't remember the
last time I needed/wanted the GenerateMock<T> behaviour (at least not
when using AAA style testing).

Hope this helps.
Regards,
David

changhong

unread,
Nov 28, 2009, 10:54:42 PM11/28/09
to Rhino.Mocks
Thank you very much for your answer.
I have made some tests and they showed exactly the behavior as what
your said.
Reply all
Reply to author
Forward
0 new messages