bloc.io:由于未知原因导致XTCAssertTest失败;以前在模拟应用中成功

时间:2015-02-26 23:43:28

标签: ios objective-c macos unit-testing

目前在bloc.io的iOS课程中。写了一个未通过测试的方法,我不知道为什么。

[背景:现在已经和它搏斗了4个小时,扫描了stackOverflow和我的课程资料,以确保我理解循环,可变字符串,附加可变字符串以及将整数放入字符串。]

使用我用来测试的基本应用实践项目,我使用NSLog编写了一个方法来验证字符串中的内容,它就像一个魅力。

当我将方法复制并粘贴到bloc.io练习时,它没有通过测试,我觉得它不应该失败。

1)最初在我的"练习应用程序"

中起作用的内容
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {

NSMutableString* numberString = [[NSMutableString alloc] init];
for(int index=-7;index<=13;index++)
{
    [numberString appendString:[NSString stringWithFormat:@"%d",index]];
}
NSLog(@"Content in MutableString: %@", numberString);
// return @"numberString";
return YES;
}

2)#1的输出:

Content in MutableString: -7-6-5-4-3-2-1012345678910111213 // so far so good

3)&#34;复制和粘贴版本&#34;适合这项练习:

- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber:(NSInteger)otherNumber
{
NSMutableString* numberString = [[NSMutableString alloc] init];
for(NSinteger index=number;index<=otherNumber;index++)
{
    [numberString appendString:[NSString stringWithFormat:@"%ld",(long)index]];
}
//    NSLog(@"Content in MutableString: %@", numberString);
return @"numberString";
}

4)失败的测试我不明白为什么:

- (void)testThatStringWorksAscending {
NSInteger lowNumber = -7;
NSInteger highNumber = 13;
NSString *expectedString = @"-7-6-5-4-3-2-1012345678910111213";
NSString *actualString = [self.counter stringWithNumbersBetweenNumber:lowNumber andOtherNumber:highNumber];
XCTAssertEqualObjects(expectedString, actualString, @"strings differed");
}

1 个答案:

答案 0 :(得分:1)

Yarrrghh。想通过我很棒的集团导师史蒂夫想出来的。我不应该使用

    return @"numberString";

而是:

return numberString;

差异是文字字符串与变量,我需要更好地理解...