【问题标题】:Strange compiler error: 'expectation' produces 'XCTestExpectation', not the expected contextual result type 'XCTestExpectation'奇怪的编译器错误:“expectation”产生“XCTestExpectation”,而不是预期的上下文结果类型“XCTestExpectation”
【发布时间】:2019-04-08 02:33:11
【问题描述】:

我正在尝试测试将运行多次的异步代码,并尝试提前为模拟用户输入创建单独的期望。我正在尝试使用一个结构,该结构将位于一个数组中,然后异步函数和主测试函数都可以访问该结构。

下面的代码虽然产生了一个我以前从未见过的编译错误,但似乎谷歌也没有

struct TestAsyncUserInput<T> {
    var value : T
    var expect : XCTestExpectation

    init (value : T) {
        self.value = value
        self.expect = expectation(description: "a random expectation")
    }
}

self.expect 显示此编译器错误: 'expectation' 产生 'XCTestExpectation',而不是预期的上下文结果类型 'XCTestExpectation'

我正在使用带有 Swift 4.2 的 Xcode 10

虽然有不同的解决方法,但我更关心这个编译器错误是什么以及为什么会发生?

更新:这是错误的屏幕截图

为了更好地衡量期望函数的定义

open func expectation(description: String) -> XCTestExpectation

【问题讨论】:

    标签: swift xcode xctest


    【解决方案1】:

    这行得通:

    struct TestAsyncUserInput<T> {
        var value : T
        var expect : XCTestExpectation
    
        init (value : T) {
            self.value = value
            self.expect = XCTestExpectation(description: "a random expectation")
        }
    }
    

    【讨论】:

    • 有趣。对于我的生活,我无法理解为什么这会起作用而我的版本不起作用,另请参阅更新的问题。知道编译器错误是什么吗?
    • 据我所知,根本错误是expectationXCTestCase 的成员函数,在TestAsyncUserInput 的初始化程序中,没有实例可以调用它。错误信息真的很混乱,我同意。
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多