I'm trying to add unit tests to an existing iOS application, using among others OCMock
.
In this application, we have a bunch of CoreData entities and generated classes. These classes obviously contain @dynamic
properties.
I tried to stub one of these properties as follows:
self.event = [OCMockObject mockForClass:[ACEvent class]];
[[[self.event stub] andReturn:@"e46e1555-d866-4160-9b42-36d0fb9c29cd"] eventGUID];
The point is, it doesn't work. Apparently because an @dynamic
property does not have an implementation by default, and in this case relies on CoreData to provide it. I end up with a NSError:
-[NSProxy doesNotRecognizeSelector:eventGUID] called!
I've seen other questions where this has been solved by abstracting the CoreData entity behind a protocol (OCMock with Core Data dynamic properties problem). But since this is an existing code base, I don't have this option, as I can't afford to refactor everything.
Can anyone provide another solution to this?
EDIT: As a side note, I just found a solution, but I'm worried it could not work in all cases. What I did is provide a sample, empty implementation for these methods in the test target. It works, but I'm worried it could break other tests that rely on CoreData to work. Any insight on this?