XCTest'ing 元组 [英] XCTest'ing a tuple
问题描述
我正在尝试像这样构建单元测试:
I am trying to build a unit test like so:
// region is a (Double, Double) tuple
XCTAssertEqual(region, (0.0, 200.0))
但是 Xcode 给了我一个错误:无法使用类型为 ((Double, Double), (Double, Double)) 的参数列表调用 'XCTAssertEqual'
But Xcode is giving me an error: Cannot invoke 'XCTAssertEqual' with an argument list of type ((Double, Double), (Double, Double))
是否有不同的方法来测试元组而不提取其成员并单独测试?
Is there a different way to test tuples without extracting their members and testing individually?
推荐答案
XCTAssertEqual
要求传递给它的两个参数是Equatable
,从方法中可以看出签名.注意expression1
返回T?
,T
必须是Equatable
:
XCTAssertEqual
requires that the two parameters passed to it are Equatable
, which you can see from the method signature. Note that expression1
returns T?
, and T
must be Equatable
:
func XCTAssertEqual<T : Equatable>(_ expression1: @autoclosure () throws -> T?, _ expression2: @autoclosure () throws -> T?, _ message: @autoclosure () -> String = default, file: StaticString = #file, line: UInt = #line)
但是 Swift 元组不是 Equatable
,因此您不能将它们与 XCTAssertEqual
一起使用.
元组do有一个 ==
方法——它们只是不符合协议——所以你可以做这样的事情:
Tuples do have an ==
method — they just don't conform to the protocol — so you could do something like this:
let eql = region == (0.0, 200.0)
XCTAssertTrue(eql)
甚至:
XCTAssertTrue(region == (0.0, 200.0))
这篇关于XCTest'ing 元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!