Skip to content

cannot write tests in package being tested due to import cycle #210

@ideasculptor

Description

@ideasculptor

counterfeiter fakes include a line at the bottom which assigns a pointer to the fake to a variable declared as type of interface being mocked. This is a great way to ensure that the counterfeit actually implements the specified interface so that the compiler will generate an error when the interface changes, but it means that you cannot implement a test that depends on the counterfeit implementation within the package itself, because it forces a dependency on that package into packagefakes package.

This means that every time we run go generate, we must also run a script which strips that line out of each generated file. There should be a command line option to skip adding that line to the counterfeit, or which specifies the file to add it to. If I want to detect that the interface is no longer satisfied, I can add such an assignment myself, rather than having the code generator break all test code that adheres to the common pattern of putting tests in the same package as the code being tested (in order to gain access to non-exported fields and methods, which can greatly simplify testing). But it would be even better to have the generator add the line to a specified file so that we can generate those checks in the package being tested rather than in the packagefakes package where it causes an import cycle.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions