qur / withmock

Automatic Go package mock generation tool
Other
71 stars 9 forks source link

withmock not usable with Gingko/Gomega #38

Closed DanielCloudCredo closed 9 years ago

DanielCloudCredo commented 10 years ago

Is it possible to use both withmock and Ginkgo in the same suite of Golang tests?

We've tried to run withmock ginkgo -r to run our tests, but it appears that withmock is polluting the namespace of the current block, presumably by doing 'dot imports' under the hood.

We get errors similar to the following:

.../broker/broker_suite_test.go:7: EXPECT redeclared during import "github.com/onsi/gomega"
    previous declaration during import "github.com/onsi/ginkgo"
qur commented 10 years ago

Hopefully it is, though I haven't tried it.

withmock doesn't add dot imports to anything. What it does do is add MOCK and EXPECT functions to packages to expose the extra functionality it adds.

However, this can cause issues with test frameworks - which often recommend using dot imports for cleaner test syntax. Two mechanisms are provided to resolve the issue (unfortunately the documentation is sorely behind):

  1. Exclude test libraries from being processed. This is the best option when the code being dot imported doesn't actually need to have mock functionality added (e.g. for test libraries). To do this you create a file listing import paths one per line (blank lines and lines starting with # are ignored), and then run "withmock -exclude /path/to/exclude/file ginkgo -r"

    see https://github.com/qur/withmock/tree/master/scenarios/excludes for an example

  2. Change the function names. You can rename the MOCK and EXPECT methods on a per packages basis using a yaml configuration file, if you feel approach 1 isn't appropriate. In this case you would run "withmock -c /path/to/config/file ginkgo -r".

    see https://github.com/qur/withmock/tree/master/scenarios/issue19 for an example.

Hopefully one of these will help you get ginkgo up and running ...