OCMock Bug: NSPredicates dont work

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.

OCMock Bug: NSPredicates dont work

Postby Mark » 30 Aug 2011, 17:09

I wrote the following Unit Test in OCMockObjectTests.m:

- (void)testNSPredicateBug
{
id mock1 = [OCMockObject mockForClass:[TestClass class]];
[[[mock1 stub] andReturn:@"match"] uid];
[[[mock1 stub] andReturn:@"match"] valueForKey:@"uid"];
id mock2 = [OCMockObject mockForClass:[TestClass class]];
[[[mock2 stub] andReturn:@"not"] uid];
[[[mock2 stub] andReturn:@"not"] valueForKey:@"uid"];

NSArray *array = [NSArray arrayWithObjects:mock1, mock2, nil];

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"uid = %@", "match"];

NSArray *matchArray = [array filteredArrayUsingPredicate:predicate];

STAssertTrue([matchArray count] == 1, @"");
}

It looks that NSPredicate converts the @"uid = "match"" such that uid becomes NSString. However the valueForKey=@"uid" converts @"uid" into NSConstantString. When OCMockRecorder is checked to see if the invocation works it checks to see if the classes are the same, however these two dont match classes.

A workaround is to use the [OCMArg checkWithSelector:@selector(isEqualToString:) onObject:@"uid"]

I wasnt sure if this is a place to log this bug, but figured others might run into this same problem.
Mark
 

Re: OCMock Bug: NSPredicates dont work

Postby Tim » 22 Jan 2012, 06:30

Thank you so much for posting this Mark - I just ran into this problem and I don't know if I ever would have figured out what was happening on my own. Your report of this problem just saved me a lot of frustration.
Tim
 

Re: OCMock Bug: NSPredicates dont work

Postby erik » 24 Jan 2012, 00:36

Just as an update: This should be fixed in OCMock now. It no longer insists that two objects are of the same class. As long as they claim to be equal... You have to get the latest version of OCMock from Github.

If it wasn't for Apple changing all the settings once more, I'd have a binary release out already. But now I need to redo the creation process for a "fat" library, and this is about as much fun as a trip to the dentist.
erik
 
Posts: 90
Joined: 10 Oct 2009, 15:22
Location: Hamburg, Germany


Return to OCMock