为什么模仿鸟可以用类型的参数找到我的存根?

发布于 2025-01-17 22:05:29 字数 1331 浏览 1 评论 0原文

我在 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
    }
}

运行后一个测试会出现以下错误:

缺少 'getNumber(_ type: T.Type) -> 的存根实现 ->带参数的 Int' [Int(通过引用)]

确保该方法具有使用返回类型注册的具体存根或默认值提供程序。

示例:
给定(someMock.getNumber(…)).willReturn(someValue)
给定(someMock.getNumber(…)).will { 返回 someValue }
someMock.useDefaultValues(来自:.standardProvider)

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

I'm using MockingBird for mocks in a Swift 5 project. A test like this passes fine:

// 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
    }
}

However, if I add a type argument, it doesn't work:

// 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
    }
}

Running the latter test gives the following error:

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)

Why doesn't this work? Is there any way I can get around this, e.g. using any()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-24 22:05:29

我想我知道这个问题。

将参数更改为yy()作为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(Int.Type.self))).willReturn(1)
        _ = mock.getNumber(Int.self) as Int
    }
}

I think I know the problem.

Change the parameter to any() as Int.Type.

The test file would look like this:

// 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(Int.Type.self))).willReturn(1)
        _ = mock.getNumber(Int.self) as Int
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文