Mocking Core Data

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Mocking Core Data

Postby jfoulkes » 23 Dec 2013, 18:26

I *think* I have created an extremely simple way to mock managed objects with OCMock. Is there anyway this would be useful in the main repo? Basically what I'm doing is subclassing OCClassMockObject and overriding:

Code: Select all
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
    NSString *sel = NSStringFromSelector(aSelector);
    if ([sel rangeOfString:@"set"].location == 0) {
        return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
    } else {
        return [NSMethodSignature signatureWithObjCTypes:"@@:"];
    }
}


There are probably a lot of things wrong with this that I haven't thought about, but it seems like it works (I've only done some very simple testing).
jfoulkes
 

Re: Mocking Core Data

Postby chejose » 31 Mar 2014, 03:52

This seems to cause problems with methods. For example, -valueForKey: will return the first -andReturn: set when passed any parameter.

Code: Select all
id mock = [OCMockObject mockWithClass:[SomeClass class]];
[[[mock stub] andReturn:@1] valueForKey:@"one"];
[[[mock stub] andReturn:@2] valueForKey:@"two"];

// [mock valueForKey:@"one"]; // returns @1
// [mock valueForKey:@"two"]; // also returns @1
chejose
 


Return to OCMock