OCMock with Core Data dynamic properties problem

前端 未结 3 789
天命终不由人
天命终不由人 2020-12-25 13:43

I\'m using OCMock to mock some Core Data objects. Previously, I had the properties implemented with Objective-C 1.0 style explicit accessors:

// -- Old Core          


        
3条回答
  •  失恋的感觉
    2020-12-25 14:25

    Also responded to your cross-post on the OCMock Forum

    Check out http://iamleeg.blogspot.com/2009/09/unit-testing-core-data-driven-apps.html.

    Basically he suggests abstracting out your Core Data object's interface to a protocol, and using that protocol instead of the class where you pass instances of your core data object around.

    I do this for my core data objects. Then you can use mockForProtocol:

    id mockItem = [OCMockObject mockForProtocol:@protocol(MyItemInterface)];
    [[[mockItem expect] andReturn:@"fakepath.pdf"] PDFName];
    

    Works great! He also suggests creating a non-core data mock implementation of the interface which just synthesizes the properties:

    @implementation MockMyItem
    @synthesize PDFName;
    @end
    
    ...
    
    id  myItemStub = [[MockMyItem alloc] init] autorelease];
    [myItem setPDFName:@"fakepath.pdf"];
    

    I've used this as well, but I'm not sure it adds anything over the mockForProtocol:/stub: approach, and it's one more thing to maintain.

提交回复
热议问题