title: OCMock: stub a @dynamic property
body: <p>I'm trying to add unit tests to an existing iOS application, using among others <code>OCMock</code>.</p>
  
  <p>In this application, we have a bunch of CoreData entities and generated classes. These classes obviously contain <code>@dynamic</code> properties.</p>
  
  <p>I tried to stub one of these properties as follows:</p>
  
  <pre><code>self.event = [OCMockObject mockForClass:[ACEvent class]];
  [[[self.event stub] andReturn:@"e46e1555-d866-4160-9b42-36d0fb9c29cd"] eventGUID];
  </code></pre>
  
  <p>The point is, it doesn't work. Apparently because an <code>@dynamic</code> property does not have an implementation by default, and in this case relies on CoreData to provide it. I end up with a NSError:</p>
  
  <pre><code>-[NSProxy doesNotRecognizeSelector:eventGUID] called!
  </code></pre>
  
  <p>I've seen other questions where this has been solved by abstracting the CoreData entity behind a protocol (<a href="https://stackoverflow.com/questions/1876568/ocmock-with-core-data-dynamic-properties-problem">OCMock with Core Data dynamic properties problem</a>). But since this is an existing code base, I don't have this option, as I can't afford to refactor everything.</p>
  
  <p>Can anyone provide another solution to this?</p>
  
  <p>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?</p>