lundi 2 février 2015

OCMock unpredictable value when testing class method parameter argument

I'm getting really unpredictable behaviour when trying to test a class method with OCMock. The problem I have is when trying to get the value of the parameter passed to the class method.


When I call the method on the mock inside my test everything works perfectly:



- (void)test {
id mockArchiver = OCMClassMock([TestArchiver class]);
[[mockArchiver class] archiveRootObject:@[@"1", @"2", @"3"]];
OCMVerify([mockArchiver archiveRootObject:[self objectChecker]]);
}

- (OCMArg *)objectChecker {

return [OCMArg checkWithBlock:^BOOL(id value) {
// value is equal to correct array
return YES;
}];
}


However the problem comes when I call the method in the actual class I'm testing:



- (void)test {
id mockArchiver = OCMClassMock([TestArchiver class]);
Archive *archive = [[Archive alloc] initWithArchiver: mockArchiver];

[archive useArchiver];

OCMVerify([mockArchiver archiveRootObject:[self objectChecker]]);
}

- (OCMArg *)objectChecker {

return [OCMArg checkWithBlock:^BOOL(id value) {
// value returns very sporadic data
return YES;
}];
}


The useArchiver is very simple...



- (void)useArchiver {

[[self.archiver class] archiveRootObject:@[@"1", @"2", @"3"]];
}


The value returned by value is very unpredictable. Sometimes it returns the correct object but most of the time it doesn't. Some example things it might return:



value __NSCFString * @"archiveRootObject:" 0x00007f80725f34f0

value __NSCFString * @"2015-02-02 10:22:47.838" 0x00007fda585e40d0

value __NSArrayM * @"1 object" 0x00007faa2158aaa0:
[0] OCMBlockConstraint * 0x7fa420632340 0x00007fa420632340

value __NSArrayI * @"3 objects" 0x00007fed53da1c50: // correct returned object
[0] __NSCFConstantString * @"1" 0x000000010b78e920
[1] __NSCFConstantString * @"2" 0x000000010b78e940
[2] __NSCFConstantString * @"3" 0x000000010b78e960


Or a EXC_BAD_ACCESS on the line that returns the OCMArg block


Aucun commentaire:

Enregistrer un commentaire