XCTAssertNoThrow 在闭包中调用时不编译

XCTAssertNoThrow doesn't compile when called in closure

尝试编译一个测试用例,其中 XCTAssertNoThrow 在闭包中使用但没有成功。怎么这么死板,是重抛还是怎么回事?

let e = expectation(description: "Wait")

distillery.produce(request: Request(bottles: 1337)) { (result) in // error
  XCTAssertNoThrow(try result.unwrap())

  e.fulfill()
}

Xcode编译时报错:

Invalid conversion from throwing function of type '(_) throws -> ()' to non-throwing function type '(Result) -> Void'

ps:这似乎是 Swift 中的一个错误,我认为 https://bugs.swift.org/browse/SR-487

通过将 XCTAssertNoThrow 包装到辅助函数中解决了这个问题

func assertOk<T>(_ result: Result<T>, file: StaticString = #file, line: UInt = #line) {
    XCTAssertNoThrow(try result.unwrap(), file: file, line: line)
}