2

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.

PALLAVI
  • 49
  • 7

1 Answers1

2

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

PALLAVI
  • 49
  • 7
  • Great workaround! To see more about this case, see this: https://stackoverflow.com/questions/53487613/dynamic-dispatching-protocol-extension-doesnt-work-multiple-targets/53488560#53488560 – J. Doe Nov 26 '18 at 21:05