【问题标题】:Objective-C Travis issue for boolean values in XCTAssertEqual: ("NO") is not equal to ("0")XCTAssertEqual 中布尔值的 Objective-C Travis 问题:(“NO”)不等于(“0”)
【发布时间】:2014-11-22 13:31:35
【问题描述】:

这是我在本地成功的测试代码:

- (void)setUp {

    restroom = [[Restroom alloc] initWithName:@"Target" andIsAccessible:FALSE andIsUnisex:TRUE];

}

- (void)tearDown {

    restroom = nil;
}

- (void)testThatARestroomCanBeCreated
{
    XCTAssertNotNil(restroom, @"Should be able to create a Restroom instance.");
}

- (void)testThatRestroomHasAName
{
    XCTAssertEqualObjects(restroom.name, @"Target", @"Restroom should have the name given when initialized.");
}

- (void)testThatRestroomHasAFlagForAccessibility
{
    XCTAssertEqual(restroom.isAccessible, FALSE, @"Restroom should have the accessibility flag given when initialized.");
}

- (void)testThatRestroomHasAFlagForUnisex
{
    XCTAssertEqual(restroom.isUnisex, TRUE, @"Restroom should have the unisex flag given when initialized.");
}

然而,当 Travis 处理它时,我只得到与布尔值相关的测试错误:

 ✗ -[RRiOSAppTests testThatRestroomHasAFlagForAccessibility] (0 ms) (0)
  -[RRiOSAppTests testThatRestroomHasAFlagForUnisex]

/Users/travis/build/.../RRiOSApp/RRiOSAppTests/RRiOSAppTests.m:91: ((restroom.isUnisex) 等于 (1)) 失败: ("YES") 不等于("1") - 洗手间应该有初始化时给出的中性标志。:

...

✗ -[RRiOSAppTests testThatRestroomHasAFlagForUnisex] (0 ms) (1)
✓ -[RRiOSAppTests testThatRestroomHasAName] (0 ms)

/Users/travis/build/.../RRiOSApp/RRiOSAppTests/RRiOSAppTests.m:86: ((restroom.isAccessible) 等于 (0)) 失败: ("NO") 不等于("0") - 洗手间应该在初始化时具有可访问性标志。

我尝试用10 替换TRUEFALSE - 但出现了同样的错误。

这是我的 .travis.yml 文件:

language: objective-c
install:
    - brew remove --force xctool
    - brew install xctool --HEAD

script:
  - xctool test -project RRiOSApp/RRiOSApp.xcodeproj -scheme RRiOSApp -sdk iphonesimulator7.0

而且,我正在使用 Xcode 6

【问题讨论】:

  • isUnisex 属性的类型是什么?

标签: objective-c xcode6 travis-ci


【解决方案1】:

BOOLYESNO,而不是 TRUEFALSETRUEYES 不是同一类型,这就是测试失败的原因。还有true,是完全不同的类型。

TRUE 在 Core Foundation 中定义为:

#define TRUE 1

YES 在 ObjC 运行时中定义为:

#define YES (BOOL)1

true 的定义不同,具体取决于您是编译为 C99 还是 C++。感兴趣的可以去stdbool.h查一下。

但无论如何,您都不应该测试与BOOL 的相等性(永远,不仅仅是在测试中)。你应该检验真理。你想要的测试是XCTAssertTrueXCTAssertFalse。有许多“真”值不等于 YESTRUE,这就是为什么在 BOOL 上测试相等性容易出错的原因。

【讨论】:

    【解决方案2】:

    我只是在阅读 TDD 教科书时自己随机找到了这个问题的答案。我不得不在 XCTAssertEqual 中转换布尔值(例如XCTAssertEqual(restroom.isAccessible, (BOOL)FALSE, @"Restroom should have the accessibility flag given when initialized.");)。这本书是 Graham Lee 的“Test-Driven iOS Development”——强烈推荐!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-19
      • 2016-01-31
      • 1970-01-01
      • 2020-01-20
      • 2013-04-06
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多