Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks: Stub & Mocks. What is the difference

I'm using Rhino Mocks in my unit test.

I would like to know the difference between STUBS and MOCKS (mocks.Stub<T>() and mocks.StrictMock<T>()).

like image 864
Yuriy Avatar asked Sep 02 '11 05:09

Yuriy


People also ask

What is the difference between mocks and stubs in Rhino?

A stub is simply an alternate implementation. A mock, however, is more than that. In Rhino.Mocks, you used the .Stub() and .Expect() extension methods to generate your stubs and mocks directly off your mock object. Moq, on the other hand, uses the .Setup() method on the wrapper object to create both.

What is an rhinoceros mock?

Rhino Mocks will generate fake objects to replace the dependencies that you have, and then allow you to tell them, at runtime, how to behave. This functionality is very powerful, and it means that you can tell your fake objects, for each test, how to behave.

What is the difference between Moq and mockrepository in Rhino?

With Rhino.Mocks, the MockRepository returns to you an instance of the type you're mocking and you apply the mock operators ( .Stub (), .Expect (), .VerifyAllExpectations (), etc) directly to that mocked object. Moq, on the other hand, creates a wrapper object that contains a reference to the mocked type.

What is the use of a library like Rhino mock?

Rhino Mocks will generate fake objects to replace the dependencies that you have, and then allow you to tell them, at runtime, how to behave. This functionality is very powerful, and it means that you can tell your fake objects, for each test, how to behave. As usual, the best way to explain what a library does is to look at the code using it.


1 Answers

I think it had been asked before.

It is generally the same with the following differences:

  • Strict Mocks throw exceptions on each call which had not been expected
  • Dynamic Mocks accept unexpected calls und just return default values (eg. null)
  • Stubs are like dynamic mocks but have "property behaviour" turned on by default. This allows writing and reading properties like fields, but doesn't allow Stub, Expect nor AssertWasCalled on properties. This behaviour can also be configured on a normal Mock too. But for stubs it is the default.

Since Rhino changed to AAA syntax, it is discouraged to use Strict Mocks, since they make test code very hard to maintain.


Similar questions:

  • What are the differences between mocks and stubs on Rhino Mocks?
  • Rhino Mocks - Difference between GenerateStub<T> & GenerateMock<T>

I can't find the information about the Rhino implementation, so I don't mark this question as duplicate.

like image 80
Stefan Steinegger Avatar answered Sep 21 '22 00:09

Stefan Steinegger