Search code examples
cocoapodsocmock

OCMock does not mock class included via Cocoapods


I'm using OCMock to mock DataFetcher class:

id dataFetcherMock = OCMClassMock([DataFetcher class]);
OCMStub([dataFetcherMock fetchSetupForCompany:[OCMArg any]
                                   completion:[OCMArg any]]).andCall(self, @selector(fetchSetupForCompany:completion:));

It was working fine when DataFetcher was a part of the project. But then we moved DataFetcher to a private Cocoapods. And the mocking stopped working. Method defined in andCall just never called anymore.

Can OCMock be used to mock classes added via cocoapods? How can I make it work?


Solution

  • I found that the problem was with the way I included testing target in pod file. What I had was:

    target 'Service' do
        pod 'MTDates'
        pod 'libextobjc'
        pod 'DataFetcher'
    
        target 'ServiceTests' do
            pod 'OCMock'
            pod 'OHHTTPStubs'
        end
    end
    

    This resulted in DataFetcher class being linked to both target and thus it was duplicated in runtime.

    I changed to

    target 'Service' do
        pod 'MTDates'
        pod 'libextobjc'
        pod 'DataFetcher'
    end
    
    target 'ServiceTests' do
        pod 'OCMock'
        pod 'OHHTTPStubs'
    end
    

    This resolved the problem.