运行时的OCMClassMock异常

时间:2015-07-27 09:57:14

标签: ios objective-c unit-testing xcode6 ocmock

我正在构建一个测试,检查标准中的每个PSColour是否为navigationColour(蓝色,棕色)。我将它设置为模拟颜色并将方法isNavigationColour结果存根为每种颜色的BOOL。

然后我给每个颜色一个ID,并将它们放入一个名为criteria的数组中,该数组传递给我的方法navigationColoursFromCriteria以返回导航颜色和数组。

当我正常运行此测试时,它会在OCMReturnValueProvider的initWithValue方法上返回异常,说EXC_BAD_ACCESS。我是OCMock的新手,并且发现我的实现没有任何问题,但另一个奇怪的事情是,如果我在测试开始时设置一个断点然后一行一行地到最后,测试运行成功。

我的测试和相关方法包括在下面:

- (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; }

我很感激有关如何改进测试并解决此问题的任何帮助或提示。

0 个答案:

没有答案