bloc.io:未知原因导致XTCAssertTest失败;之前在mock应用程序中获得成功



目前在block.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) 适用于本练习的"复制粘贴版本":

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

Yarrghh。多亏了我出色的团队导师史蒂夫,我才明白了这一点。我不应该用

    return @"numberString";

而是:

return numberString;

区别是文字字符串与变量,我需要更好地理解。。

最新更新