Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference in OCMock expect and stub methods?

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?

like image 438
sanjana Avatar asked Jun 19 '12 07:06

sanjana


1 Answers

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.

  1. 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.
  2. 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).

like image 168
Christopher Pickslay Avatar answered Oct 11 '22 11:10

Christopher Pickslay