我有一个协议
protocol SomeProtocol { func method() }
及其实施
extension SomeProtocol {func method(){--implementation--}}
在构建目标中,我有一个类确认此协议
class SomeClass: SomeProtocol { func doSomething() { method() } }
我想要的是我想要在我的测试目标中的 XCTest 文件中对协议方法进行不同的实现。为此,我所做的是扩展 SomeClass 并在那里编写我的实现。
extension SomeClass {func method(){--other implementation--} }
但在执行测试用例时它从未被调用。始终会调用构建目标中的方法(默认实现)。
请建议我应该做什么。
找到了。我正在使用@testable import MYProject.
如果您使用此方法,则上述方法将不起作用。如果您要添加所有项目文件而不是使用导入,则上述方法有效。
ref: https://medium.com/@hartwellalex/protocol-extensions-and-shared-dependency-injection-in-swift-an-alternative-to-singletons-68934dee6998 https://medium.com/@hartwellalex/protocol-extensions-and-shared-dependency-injection-in-swift-an-alternative-to-singletons-68934dee6998
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)