Question

I am learning OCMock in iOS.Could anybody let me know whats the difference between "niceMockForClass" "partialMockForObject" and "mockForClass" method. How and when to use all the three.I have created a custom class named "CustomClass". It has a method "verifySetUp". Now, I need to test this?

-(void)testVerifyMethod {
    CustomClass *cc = [[CustomClass alloc] init];
    id mock = [OCMockObject partialMockForObject:cc];
    [[mock stub] verifySetUp];
    [mock verify];
}

When changing the partialMockForObject to niceMockForClass or mockForClass tests the method successfully. Now, I just want to when and how to use all the three.

How to create a Mock for the connection in OCMock and how can I use it?

Was it helpful?

Solution

All of these features are documented pretty clearly on the OCMock website. There is nothing special about mocking iOS7 as opposed to any other Objective-C code. Before you worry about the difference between these different methods of mocking you might want to consider why you want to mock at all.

You want to test your class and specifically a method called "verifySetUp". Ask yourself, what does that method do? What input does it accept and what is the expected result when it is complete? In your tests, call this method directly and afterward assert that what you want to happen happens. Look at STAssert or XCTAssert for making assertions about your expectations.

Mocking lets you stub out functionality beyond the unit under test. Suppose the method you are testing makes a call to another method. You might only want to verify this method is called, you might want to fake the method call to return data of your choosing, or you might simply want to prevent the method from executing because it is not relevant to the test. In these cases, you could consider using mocks.

The documentation covers this well, but generally you use partialMocks when you wish to mock some of an object's methods but have others use their original implementation, objectMocks when you care about every interaction with the mocked object, and niceMocks when you need to mock certain functionality of an object but are willing to have other calls to the object do nothing but not fail.

Finally, again I don't know what 'verifySetUp' does, but its name suggests that it itself is a test. If your intention is write test coverage for a method that does some 'set up', you might want to be calling your set up method directly in your test.

Example:

- (void)test_setUp_shouldStartEngine_andSetWidgetReadyStateToYes
{
    // Set up a mock to verify a method will be called by our method under test.
    id engineMock = [OCMock niceMockForClass:Engine.class];
    [[engineMock expect] start];

    // Create a real object that will be manipulated by our method under test.
    Widget *widget = [Widget new];

    // Do the work
    [AppController setUpEngine:engineMock widget:widget];

    // Verify the widget is ready
    XCTAssertTrue(widget.ready, @"Widget not ready");

    // Verify engine's start method was called
    [engineMock verify];
}

I hope this helps. Happy unit testing; it is a great way to make sure your code does what you intended it to do!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top