-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Import cycle when using the fake in the same package as the interface #68
Comments
Sorry this caused you some pain @nkovacs. Unfortunately, I'm not really amenable to changing it. For one, I feel this adds some clarity to the generated code that it actually does implement the desired interface - if you changed the interface, but did not change your generated code, you would see some compile errors in your tests - which is not at all where the problem is. Leaving this assignment here gives you a single compile error in the implementation that is closer to the source of the problem (namely, the fake doesn't implement the interface). In general, I am strongly against the idea of using a generated fake within the same package. That seems to me to be opposed to the philosophy of go, which is to have separate packages for separate concepts. With interfaces, I can't imagine a strong compelling reason to have the definitions for two interfaces arising from the same package, but wanting to mock them between their tests. Perhaps that's just your tests providing feedback on your implementation that those should be separate. Or perhaps they shouldn't be mocked that way. If you really want to do this, I suggest you write a small script that removes the assignment at the end of the file. Or you could fork counterfeiter. |
Sorry @nkovacs, I don't really follow. Is the precise problem here that the artist unit test cannot use a counterfeiter double? The bigger issue there, as I see it, is that the unit test is in the Go, as a language and tool, does not ensure that one writes good tests. Nor does it ensure that one's tests are testing the right things. One nice thing about open source projects is that they can make decisions that help guide users to do the right thing. In my opinion, it's correct to have tests in the For the other 1% of cases, they should be so exceptional, that it shouldn't be quite so easy to do, in order to encourage users to test their interfaces "properly". Granted, this is subjective, and not an absolute truth. |
Yes, the problem is that I cannot generate a fake for the And yes, using a Actually I'd prefer having the fakes generated into the same package (that would also fix #8), like in the original, hand-crafted code. These fakes are really only useful for the tests that use them, so I don't see why they need to be in a separate package. In a dynamic language, you'd be creating those fakes in the tests, at runtime. You wouldn't have them as a separate class, ready to import in all the tests that need them. In this comment you brought up |
What if I want to test unexported stuff? Anyway, I solved this by making it possible to generate the fake into the same package, since that's actually what I need. |
Hi @nkovacs , I have the same issue. Are you still using your fork? |
If you can make the interface private it won't add the interface assertion to the bottom of your fake |
i don't see the -pkg flag in the main build. is the fix not merged ? |
This causes an import cycle:
This is caused by the interface check at the bottom of fake_fooer.go:
One solution is to change the test file's package to
counterfeit_tests
, but that doesn't work if I want to test unexported functions.#8 is similar, but that one is solvable by renaming the import. This isn't.
Is this check really necessary though? If you try to use the fake in a test, and it no longer satisfies the interface, it'll fail to compile anyway (well, except if you're doing something tricky with interface{}).
Could you maybe add an option to disable this check?
The text was updated successfully, but these errors were encountered: