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?

+3


source to share


2 answers


It is generally confused to mock the class under test, but if you want to do this, you need a "partial mock" so you can call methods without biting them and make them execute normal methods.



This is similar to the support in OCMock according to the docs .

+2


source


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];
}

      

+2


source







All Articles