0

As far as I know, Stub is just a replacement for a dependency. Stub is not used for verificiation and it cannot fail a test -from the book The Art Of Unit Testing.

But it seems since Rhino Mocks 3.5 I can do this:

var service = MockRepository.GenerateStub<ILuckyService>(); 
service.AssertWasCalled(s=>s.GetLuckyNumberOfTheDay());

If can verify whether a method is called on a stub, then why do I need to bother myself if I need to use a stub or mock?

pencilCake
  • 51,323
  • 85
  • 226
  • 363
  • Because of clean code reasons? – Grrbrr404 Dec 22 '11 at 10:49
  • Probably they're the same: see this question: http://stackoverflow.com/questions/477924/rhino-mocks-stub-expect-vs-assertwascalled and also this Ayende's post: http://ayende.com/blog/3384/rhino-mocks-3-5-design-decisions-the-role-of-stub-vs-mock – Amittai Shapira Dec 22 '11 at 10:52
  • Other mocking frameworks such as FakeItEasy and NSubstitute are only using single term mock/fake/stub. After all usually you just want to fake something in your unit tests and you don't care about the differences in terms. – Toni Parviainen Dec 22 '11 at 12:02

5 Answers5

3

Use Martin Fowler's article as the shared reference within your team. The distinction is important for the readers of your test : When I see a stub, I ignore it and move on.

  • A stub is just there to make your test work (NOP methods / return some canned value). It is not the focus of the test, rather an incidental detail. (Albeit a mandatory detail/dependency for your test scenario)
  • A mock on the other hand is your primary focus... you want to test whether your action resulted in the specific expectation being met on the (mocked) dependency.

That's my take on it. Even if Rhino Mocks allows you to do this, I wouldn't assert on a Stub.

Gishu
  • 134,492
  • 47
  • 225
  • 308
  • I agree with you. But are we able to do anyhting we can do with MOCK objects also with STUBS in this case? or one has some limitations over the other? – pencilCake Dec 22 '11 at 15:42
  • 1
    I've just been reading up and it seems that there is no clear distinction. Here's what it says in the docs - http://ayende.com/wiki/Rhino%20Mocks%203.5.ashx#Thedifferencebetweenstubsandmocks Seems to recommend using Stubs. Also Expect()-Do()-Verify() & AssertWasCalled seem to be 2 ways of doing the same thing. Read the underlined section at that link on when to move up to a Rhino Mock. Verdict: too complicated. – Gishu Dec 22 '11 at 18:05
  • 1
    100% agreed on "too complicated". The last thing I want when I write a test is to wonder whether the test is really doing what I think it does. Either stick to the established definition of Stub and Mock, or don't use this distinction at all in the framework. – Mathias Dec 26 '11 at 23:11
1

Actually the behavior of Mock and Stub classes in RhionMocks is the same, and it may be confusing, even to the creator of RhinoMocks.
But since there's a fundemental difference between stub and mock, selecting one over the other with Rhino Mocks, makes your code intentions more clear.
When you create a stub, you just don't want your test to fail on the method calls of the stub (e.g. when you use some Log object).
When you create a Mock, you want verify that your code used the provided object as you expected it (e.g. writing the correct data to database).

Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54
  • I use Stubs + AssertWasCalled() to verify if method has been called and think that it is a better approach that Verify(). At least because it keeps my tests in Arrange/Act/Assert shape. – the_joric Dec 22 '11 at 11:10
1

I always try to use Stubs. the difference between Stubs and Mocks (from RM point of view) is that stub will NEVER throw an exception during call. Mocks (especially Strict ones) will throw immediatelly if call is made with other arguments than expected. Also you can verify expectations made only on mocks, not stubs.

Another dilemma is what to use: Expect.Call() or SetupResult.For() (I tend to use latter). But I agree that all that richness is useless in most scenarios. That's why I prefer Stubs.

However I would recommend to take a look at Nsubstitute -- it does not have all that mess and have very expressive syntax. Just take a look at few examples and you'll start loving it immediatelly :)

the_joric
  • 11,986
  • 6
  • 36
  • 57
  • That is not exactly accurate: you can verify stubs by calling AssertWasCalled, and non-strict mock won't throw exceptions if the call is not expected, same as stub. – Amittai Shapira Dec 23 '11 at 10:43
0

See Fowler: http://martinfowler.com/articles/mocksArentStubs.html

Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
  • Martin's classification does not apply well to RhinoMocks' interpretation of Mocks and Stubs. In RM difference is much less obvious. – the_joric Dec 22 '11 at 11:09
0

Stub or Mock are concepts. You can create a stub and use it as a mock and vice-versa. I strongly recommend reading this article - http://martinfowler.com/articles/mocksArentStubs.html

P.K
  • 18,587
  • 11
  • 45
  • 51