为什么模仿鸟可以用类型的参数找到我的存根?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我知道这个问题。
将参数更改为
yy()作为int.type
。测试文件看起来像这样:
I think I know the problem.
Change the parameter to
any() as Int.Type
.The test file would look like this: