objective-ciosxcodeocmock

OCMock - is a call to "verify" needed to assert that a method is called on the mock object?


I'm confused as to what the method "verify" on OCMockObject does, is it necessary to call this to test whether a method was called on the mock object? Even if I don't actually call "verify" after the test was executed, I will still get test failures if I don't fully set the expectations for the methods to be called on the mock object.

For example:

In the test method

OCMockObject *mockView = [OCMockObject mockForClass:[UIView class]];
[controller setValue:mockView forKey:@"sampleView"];
[[mockView expect] setHidden:YES];
[controller processSampleView];
//do I need to call "verify" here?
//    If I don't, I still get test failures if "setHidden:YES" is not called...
[mockView verify];

Solution

  • The features description on the OCMock site has this:

    "The verify method will raise an exception if the expected method has not been invoked."

    and this:

    "When a method is called on a mock object that has not been set up with either expect or stub the mock object will raise an exception. This fail-fast mode can be turned off by creating a "nice" mock:"

    So, yes, exceptions will be raised when you call unexpected methods. However, the example above suggests an exception is raised when an expected method is not called even when verify is not called. That doesn't seem possible because there's no codepath into OCMock if you don't call verify. In your case, where is the exception raised from, i.e. what's the stacktrace for the exception? Is it possible that you reuse the mock view in another test that does call verify? Does the controller instance stay around between tests?