Page 1 of 1

Mocking Core Data

PostPosted: 23 Dec 2013, 18:26
by jfoulkes
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).

Re: Mocking Core Data

PostPosted: 31 Mar 2014, 03:52
by chejose
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