【问题标题】:Generic parameter 'T' could not be inferred when XCTAssertEqual() called调用 XCTAssertEqual() 时无法推断通用参数“T”
【发布时间】:2020-04-20 13:15:43
【问题描述】:

主题标题是 testPrimesUpTo100ShouldBe25() 方法中的 xCode Assert Equal 测试相关的编译器错误。

我注意到的一件事是,当从 testPrimePerformance() 和 testPrimesUpTo100ShouldBe25() 调用 PrimeCalculator 中的 calculate() 方法时,只有在 testPrimePerformance() 中“着色”为绿色。

完整的swift源文件如下:

import XCTest

struct PrimeCalculator {
    static func calculate(upTo max: Int) -> [Int] {
        guard max > 1 else {
            return []
        }

        var sieve = [Bool](repeating: true, count: max)

        sieve[0] = false
        sieve[1] = false

        for number in 2 ..< max {
            if sieve[number] == true {
                for multiple in stride(from: number * number, to: sieve.count, by: number) {
                    sieve[multiple] = false
                }
            }
        }

        // collapse our results down to a single array of primes
        let primes = sieve.enumerated().compactMap { $1 == true ? $0 : nil }
        return primes
    }
}

class AsynchronousTests: XCTestCase {

    func testPrimePerformance() {
        measure {
            _ = PrimeCalculator.calculate(upTo: 1_000_000)
        }
    }

    func testPrimesUpTo100ShouldBe25() {
        // given
        let maximumCount = 100

        // when
        let progress = PrimeCalculator.calculate(upTo: maximumCount) {
            XCTAssertEqual($0.count, 25)
        }
        // then
        let predicate = NSPredicate(format: "@.completedUnitCount == %@", argumentArray: [progress, maximumCount])

        let expectation = XCTNSPredicateExpectation(predicate: predicate, object: progress)
        wait(for: [expectation], timeout: 10)
    }

    override func setUp() {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }

    func testPerformanceExample() {
        // This is an example of a performance test case.
        measure {
            // Put the code you want to measure the time of here.
        }
    }
}

【问题讨论】:

  • 这是一个“请解决我的域特定编译错误”的问题。不适合这个论坛。

标签: swift xcode xctest xctestcase


【解决方案1】:

这里

let progress = PrimeCalculator.calculate(upTo: maximumCount) {
    XCTAssertEqual($0.count, 25)
}

您将“尾随闭包”传递给calculate() 方法,这有两个问题:

  • 首先,该方法只接受一个整数参数,而不是一个闭包参数。这是真正的问题。
  • 其次,编译器无法从上下文中判断$0的类型。这会导致编译器错误消息。

你可能想要的只是

let progress = PrimeCalculator.calculate(upTo: maximumCount)
XCTAssertEqual(progress.count, 25)

【讨论】:

    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多