Search code examples
iosobjective-cocmock

Cannot Stub method "title" because no such method exist in the mocked class OCMock


I'm writing this unit test:

   - (void)testCellValues
{
    id bookMock = [OCMockObject mockForClass:[LBBook class]];
    [[[bookMock stub] andReturn:@"Book Title"] title];
}

Of course there is some code after that. But at this point I'm having this error when I execute the test:

[LBReviewsDashboardDataSourceTest testCellValues] failed: OCMockObject[LBBook]: cannot stub or expect method 'title' because no such method exists in the mocked class.

But LBBook class has the attribute title. This is the LBBook Class:

@interface LBBook : LBManagedObjectFactory

@property (nonatomic, retain) NSString * title;

@end

I cannot realise what is happening here, why Does OCMock return that error if the class has title attribute?

Thanks in advance.


Solution

  • CoreData uses dynamic properties. These are not yet properly supported in OCMock. It is possible to stub valueForKey: instead. See also: OCMock: stub a @dynamic property and OCMock with Core Data dynamic properties problem