mock icon indicating copy to clipboard operation
mock copied to clipboard

Allow Generated Mocks to Accept Other Mock Interfaces as Input to Avoid Cyclic Imports

Open kAvEh-- opened this issue 1 year ago • 2 comments

Requested feature Consider two interfaces, InterfaceA and InterfaceB. InterfaceA has a function that takes an object of InterfaceB as input. I want to generate MockInterfaceA, with the function accepting MockInterfaceB as input instead of InterfaceB.

Why the feature is needed If the generated mock files are placed in a separate folder, and the mocked interface is used in a test file located in the main package, it will result in a cyclic import: main package -> mock package -> main package.

kAvEh-- avatar Oct 02 '24 13:10 kAvEh--

Here is a simple example why this does not work: example.zip

TL;DR: // compilation error // Type HumanMock does not implement Human // need the method: Call(dog Dog) // have the method: Call(_ DogMock) var human Human = mock.HumanMock{}

tulzke avatar Oct 02 '24 18:10 tulzke

Possible solutions to the problem:

  1. Generate mocks to the same directory as interfaces.
  2. Use the _test postfix for package in tests. Then the dependency graph will look like this:
flowchart LR

some_package
mock --> some_package
some_package_test --> some_package
some_package_test --> mock

tulzke avatar Oct 02 '24 18:10 tulzke