0
- (void)testStringExample {

    // Given
    NSString *testString = @"Test";

    id mock = OCMClassMock([NSString class]);
    OCMStub([mock stringWithContentsOfFile:OCMOCK_ANY
                              encoding:NSUTF8StringEncoding
                                 error:nil]).andReturn(testString);

    // When
    NSString *result = [NSString stringWithContentsOfFile:@"SomeFilePath"
                                             encoding:NSUTF8StringEncoding
                                                error:nil];

    // Then
    XCTAssertEqualObjects(result, testString);
}

As you can see I've been trying to stub an NSString class method using OCMock but don't seem to be having any luck. The test fails indicating that "(null)" is not equal to "Test" but I'm not sure why the mocked method isn't being called correctly. I'm using OCMock 3.1.2 on iOS. Any help greatly appreciated.

Andy Bargh
  • 46
  • 4

1 Answers1

0

Having done a bit more reading, I suspect I've got a similar issue to that shown in the following post:

How to mock class method (+)?

I've subsequently adopted the approach suggested by Christopher Pickslay in the post above and it works a treat.

Community
  • 1
  • 1
Andy Bargh
  • 46
  • 4