为什么 MockingBird 找不到带有类型参数的存根?

Why can't MockingBird find my stub with a type argument?

我在 Swift 5 项目中使用 MockingBird 进行模拟。这样的测试顺利通过:

// In app
protocol SomeProtocol {
    func getNumber() -> Int
}

// In tests
import Mockingbird
import XCTest
@testable import MyApp

class Test: XCTestCase {
    func testExample() throws {
        let mock: SomeProtocolMock! = mock(SomeProtocol.self)
        given(mock.getNumber()).willReturn(1)
        _ = mock.getNumber() as Int
    }
}

但是,如果我添加类型参数,它不起作用:

// In app
protocol SomeProtocol {
    func getNumber<T>(_ type: T.Type) -> Int
}

// In tests
import Mockingbird
import XCTest
@testable import MyApp

class Test: XCTestCase {
    func testExample() throws {
        let mock: SomeProtocolMock! = mock(SomeProtocol.self)
        given(mock.getNumber(Int.self)).willReturn(1)
        _ = mock.getNumber(Int.self) as Int
    }
}

运行后面测试报错如下:

Missing stubbed implementation for 'getNumber(_ type: T.Type) -> Int' with arguments [Int (by reference)]

Make sure the method has a concrete stub or a default value provider registered with the return type.

Examples:
given(someMock.getNumber(…)).willReturn(someValue)
given(someMock.getNumber(…)).will { return someValue }
someMock.useDefaultValues(from: .standardProvider)

为什么这行不通?有什么办法可以解决这个问题,例如使用 any()?

我想我知道问题所在了。

将参数更改为any() as Int.Type

测试文件如下所示:

// In tests
import Mockingbird
import XCTest
@testable import MyApp

class Test: XCTestCase {
    func testExample() throws {
        let mock: SomeProtocolMock! = mock(SomeProtocol.self)
        given(mock.getNumber(any() as Int.Type)).willReturn(1)
        _ = mock.getNumber(Int.self) as Int
    }
}