protocol SomethingDeliverable { func fetchSomething(somethingID: String,associatedList: String) -> Observable<String>}extension SomethingDeliverable { func fetchSomething(somethingID: String,associatedList: String) -> Observable<String> { return create { observer in return nopdisposable.instance } }}
为了测试,我们有这个测试代码:
private class StubSomethingAPI: SomethingDeliverable { var responseClosure: ((AnyObserver<String>) -> VoID)? func fetchSomething(somethingID: String,associatedList: String) -> Observable<String> { return create { observer in if let responseClosure = self.responseClosure { responseClosure(observer) } return nopdisposable.instance } }}
我们希望返回Observable< T>而不是返回Observable< String>的fetchSomething.并让StubSomethingAPI成功覆盖它以测试sakes.
我们怎么做?
protocol SomethingDeliverable { associatedtype Element func fetchSomething(somethingID: String,associatedList: String) -> Observable<Element>}extension SomethingDeliverable { func fetchSomething(somethingID: String,associatedList: String) -> Observable<Element> { return create { observer in return nopdisposable.instance } }}private class StubSomethingAPI<T>: SomethingDeliverable { typealias Element = T var responseClosure: ((AnyObserver<T>) -> VoID)? func fetchSomething(somethingID: String,associatedList: String) -> Observable<T> { return create { observer in if let responseClosure = self.responseClosure { responseClosure(observer) } return nopdisposable.instance } }}总结
以上是内存溢出为你收集整理的swift – 如何覆盖Observable for Testing的清酒?全部内容,希望文章能够帮你解决swift – 如何覆盖Observable for Testing的清酒?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)