lundi 27 juillet 2015

OCMClassMock Exception At Runtime

I'm currently building a test that checks if each PSColour within a criteria is a navigationColour (Blue, Brown). I have it set up to mock the colour and stub the method isNavigationColour result to be a BOOL for each colour.

I then give each of these colours an ID and put them into an array called criteria which is passed into my method navigationColoursFromCriteria to return and array of navigation colours.

When I run this test normally it returns an exception on the initWithValue method of OCMReturnValueProvider saying EXC_BAD_ACCESS. I'm new to OCMock and can't find anything wrong with my implementation but another weird thing is that the test runs successfully if I set a breakpoint at the start of the test and then go line by line to the end.

My test and relevant methods are included below:

- (void)testNavigationColourWithCriteria_WithColourIdArray_ShouldReturnColoursInCorrectOrder {
PSCriteria *criteria = [[PSCriteria alloc] init];

id blueColour = OCMClassMock([PSColour class]);
[[[blueColour stub] andReturnValue:@YES] isNavigationColour];
OCMStub([blueColour localizedNames]).andReturn(@{@"en" : @"Blue"});

id brownColour = OCMClassMock([PSColour class]);
[[[brownColour stub] andReturnValue:@YES] isNavigationColour];
OCMStub([brownColour localizedNames]).andReturn(@{@"en" : @"Brown"});

id blackColour = OCMClassMock([PSColour class]);
[[[blackColour stub] andReturnValue:@NO] isNavigationColour];
OCMStub([blackColour localizedNames]).andReturn(@{@"en" : @"black"});

id yellowColour = OCMClassMock([PSColour class]);
[[[yellowColour stub] andReturnValue:@NO] isNavigationColour];
OCMStub([yellowColour localizedNames]).andReturn(@{@"en" : @"yellow"});

id colourMock = OCMClassMock([PSColour class]);
OCMStub([colourMock colourWithColourId:1]).andReturn(blueColour);
OCMStub([colourMock colourWithColourId:2]).andReturn(brownColour);
OCMStub([colourMock colourWithColourId:3]).andReturn(blackColour);
OCMStub([colourMock colourWithColourId:4]).andReturn(yellowColour);

criteria.colourIds =   @[@1,@2,@3,@4];

NSArray *navigationColours = [criteria navigationColoursFromCriteria:criteria];
NSArray *result = @[@"Blue",@"Brown"];
XCTAssertEqualObjects(navigationColours, result); }


   - (NSMutableArray *)navigationColoursFromCriteria:(PSCriteria *)criteria {

navigationColoursFromCriteria:

NSMutableArray *navigationColours = [NSMutableArray arrayWithCapacity:criteria.colourIds.count];

for (NSNumber *colourId in criteria.colourIds) {
    PSColour *colour = [PSColour colourWithColourId:[colourId integerValue]];
    if (colour.isNavigationColour) {
        [navigationColours addObject:colour.localizedNames[@"en"]];
    }
}
return navigationColours; }

I'd appreciate any help or tips on how my test could be improved and this problem fixed.

Aucun commentaire:

Enregistrer un commentaire