即使在XCTest文件中的类扩展中实现了方法后,总是会调用协议的默认实现 [英] Always the default implementation of protocol gets called even after implementing the method in class extension in an XCTest file

查看:92
本文介绍了即使在XCTest文件中的类扩展中实现了方法后,总是会调用协议的默认实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个协议

protocol SomeProtocol { func method() }

及其实现

extension SomeProtocol {func method(){--implementation--}}

在构建目标中,我有一个确认此协议的类

In build target i have a class confirming to this protocol

class SomeClass: SomeProtocol { func doSomething() { method() } }

我想要的是我想在我的XCTest文件的测试目标中使用不同的协议方法实现.为此,我扩展了SomeClass并在其中编写了实现.

What i want is i want to have a different implementation of the protocol method in my test target, in my XCTest file. For that what i did was i extended the SomeClass and wrote my implementation there.

extension SomeClass {func method(){--other implementation--} }

但是在执行测试用例时从未调用过它.始终会调用构建目标(默认实现)中的方法.

But it never got called while executing test cases. Always the method in build target, the default implementation, was getting called.

请告知我应该怎么做.

推荐答案

找到了它.我正在使用@testable import MYProject.如果您正在使用上述方法,则上述方法将无法工作.如果要添加所有项目文件而不是使用导入,则可以使用上述方法.

Found it. I was using @testable import MYProject. The above method wont work if you are using this. If you are adding all of your project files instead of using the import, then the above method works.

ref: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆