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?
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.
- Unexpected invocations. When a mock object receives a message that hasn't been either stubbed or expected, it throws an exception immediately and your test fails.
- Expected invocations. When you call
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).
© 2022 - 2024 — McMap. All rights reserved.