Reputation: 203
I am trying to use OCMock for testing my app. But I am confused where should we use expect and where to use stub? Can anyone help please?
Upvotes: 8
Views: 3817
Reputation: 17772
The basic difference is this: you expect
things that must happen, and stub
things that might happen.
There are 2 ways mock objects fail: either an unexpected/unstubbed method is called, or an expected method is not called.
verify
on your mock (generally at the end of your test), it checks to make sure all of the methods you expected were actually called. If any were not, your test will fail.There are a couple of types of mocks that change this behavior: nice mocks and partial mocks. Nice mocks prevent you having to stub methods--basically they let unexpected invocations occur. Partial mocks are a way of intercepting messages sent to actual objects. Any messages you expect or stub on a partial mock will be sent to the mock object. All other messages are sent to the actual object. For both nice mocks and partial mocks, you won't get a test failure on unexpected invocations (rule #1 above).
Upvotes: 18