Skip to content

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

Closed

Description

@kAvEh--

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.

Activity

  1. changed the title [-]Generated Mocks in Separate Package Cause Cyclic Import When Used in Main Package[/-] [+]Allow Generated Mocks to Accept Other Mock Interfaces as Input to Avoid Cyclic Imports[/+] on Oct 2, 2024
  2. tulzke commented on Oct 2, 2024

    @tulzke
    Contributor

    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{}

  3. tulzke commented on Oct 2, 2024

    @tulzke
    Contributor

    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
    
    Loading
  4. locked and limited conversation to collaborators on Oct 15, 2024
  5. converted this issue into a discussion #212 on Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions