Mock class whose methods are called in the test case
So, I have a class for which I have written some test cases. This class has the following two methods:
- (void)showNextNewsItem {
self.xmlUrl = self.nextNewsUrl;
[self loadWebViewContent];
}
- (void)showPreviousNewsItem {
self.xmlUrl = self.previousNewsUrl;
[self loadWebViewContent];
}
Could be refactored, which is pretty primitive, but nevertheless, I just want to make sure that the next download of the next and previous download precedes. So I use OCMock to instantiate OCMockObject
my SUT class like this:
- (void)testShowNextOrPreviousItemShouldReloadWebView {
id mockSut = [OCMockObject mockForClass:[NewsItemDetailsViewController class]];
[[[mockSut expect] andReturn:@"http://www.someurl.com"] nextNewsUrl];
[[mockSut expect] loadWebViewContent];
[[[mockSut expect] andReturn:@"http://www.someurl.com"] previousNewsUrl];
[[mockSut expect] loadWebViewContent];
[mockSut showNextNewsItem];
[mockSut showPreviousNewsItem];
[mockSut verify];
}
The problem is with the two lines actually calling methods that do something to check. OCMock now says to me that the challenge showNextNewsItem
and showPreviousNewsItem
is not expected. Of course this was not expected because I am in testing and I only expect some things to happen in the production code itself.
What part of the mocking concept am I missing correctly?
source to share
I found a solution. Using partialMock on an object does exactly what I want. So the calls that I explicitly define are mocked and I call methods that are under testing on a "non-mocked" object.
- (void)testShowNextOrPreviousItemShouldReloadWebView {
NewsItemDetailsViewController *sut = [[NewsItemDetailsViewController alloc] init];
id mockSut = [OCMockObject partialMockForObject:sut];
[[[mockSut expect] andReturn:@"http://www.someurl.com"] nextNewsUrl];
[[mockSut expect] loadWebViewContent];
[[[mockSut expect] andReturn:@"http://www.someurl.com"] previousNewsUrl];
[[mockSut expect] loadWebViewContent];
[sut showNextNewsItem];
[sut showPreviousNewsItem];
[mockSut verify];
}
source to share