OCMock with Core Data dynamic properties problem

前端 未结 3 799
天命终不由人
天命终不由人 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:27

    One of solutions is using a protocol, which is intended to substitute it's original interface, but it could be a bit heavy and leads to significant amount of code you should duplicate.

    Personally, I found a way to make it lightweight:

    Create a simple category, for instance, inside your unit testing file, just before your unit testing class:

    @implementation MyItem(UnitTesing)
    - (NSString *)PDFName{return nil;};
    @end
    

    Also, you can keep it in separate file, but make sure, that this file is not a part of your production target. That is why I prefer to keep it in the same test file, where I want to use it.

    The huge advantage of this method, is that you should not copy methods, that are created by XCode to support relationships. Also you can put to this category only methods you are going to call inside your tests.

    There are some caveats, though, for example, you should add another methods inside the category, to support setters, when you are going to check, how correct your code changes the properties of your managed object:

    - (void)setPDFName:(NSString *)name{};
    

提交回复
热议问题