代码之家  ›  专栏  ›  技术社区  ›  Nick Bedford

直接通过getter时swift xctassertequal(float)失败

  •  0
  • Nick Bedford  · 技术社区  · 6 年前

    我在xcode的swift单元测试中遇到了一个非常奇怪的错误。当我储存 Float 一个getter的值,它起作用,但是当我把相同的getter传递给 XCTAssertEqual ,它失败,因为xct函数接收的值是 0 ,而不是实际计算值(在本例中 800 )

    有人碰到这个吗?

    // Rectangle.width = get { return left - right }
    
    let w = metrics.imageDestinationRectangle.width
    let h = metrics.imageDestinationRectangle.height
    
    // these work okay
    XCTAssertEqual(w, 800, accuracy: 0.001) 
    XCTAssertEqual(h, 533, accuracy: 0.001)
    
    // these fail: "0" is not equal to "800"
    XCTAssertEqual(metrics.imageDestinationRectangle.width, 800, accuracy: 0.001)
    XCTAssertEqual(metrics.imageDestinationRectangle.height, 533, accuracy: 0.001)
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Nick Bedford    6 年前

    嗯,我不知道发生了什么,但我做了一些其他相关的编码,然后尝试将getter直接放入xctassertequal参数,结果成功了。所有代码都没有改变。奇怪!