问题
I have a protocol
protocol SomeProtocol { func method() }
and its implementation
extension SomeProtocol {func method(){--implementation--}}
In build target i have a class confirming to this protocol
class SomeClass: SomeProtocol { func doSomething() { method() } }
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.
Please advice what i should be doing.
回答1:
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: https://medium.com/@hartwellalex/protocol-extensions-and-shared-dependency-injection-in-swift-an-alternative-to-singletons-68934dee6998
来源:https://stackoverflow.com/questions/49521742/always-the-default-implementation-of-protocol-gets-called-even-after-implementin