Guide on Using OCMock
OCMock is a great framework for Objective-C (iOS or Mac OS) for test driven development.
It changes the behaviours of classes during testing.
This is a guide on how to use OCMock, and also raise some of the pitfalls.
1. Mock an instance method
If you have a class MyClass
, an instance anObject
, and an instance method foo
, you can change the return value of foo
during testing:
1 2 3 4 5 6 7 8 9 10 11 |
|
2. Mock a class method
Mocking a class method is exactly the same as mocking for instance method.
1 2 |
|
3. Mock an object (Partial mock)
If you have the instance anObject
, you can use OCMPartialMock
to create the mock.
This mock in effect is the same as mocking the class.
1 2 |
|
Pitfall: You might assume partial mock applies to only that instance anObject
. That’s wrong. If you have anotherObject2
, it will be affected by whatever was mocked because partial mock actually mock the whole class.
1 2 |
|
4. Stop mocking
You can stop mocking to return to the real object and reset it’s behaviours.
1 2 3 |
|
We use the example of partial mock, but you could call [MyClass stopMocking]
and it will have the same effects.
5. Stub methods and chaining
You can stub the methods with different actions: return object, return native values, throw, post notification, forward to real object or do nothing.
1 2 3 4 5 6 |
|
You can also chain andPost
and andReturn
with other actions.
1
|
|
Limitations – Especially Core Data
Not only those listed on OCMock, but there are other limitations.
The most severe is that you cannot mock a NSManagedObject
.
There is a way to get around it, but it is better that you never, never mock Core Data.