Coder Social home page Coder Social logo

Comments (8)

tjarratt avatar tjarratt commented on May 28, 2024 7

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 services package, instead of in the services_test package. Granted, there's not always a lot of alignment in the Go community here about when to have unit tests in the same package, and when to use the _test package, but as a maintainer of Counterfeiter, I really want to take a hard line here.

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 _test package 99% of the time to ensure that you are testing your public interface and that you observe behavior and side effects, not implementation details. Hence counterfeiter should support that testing pattern primarily.

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.

from counterfeiter.

nkovacs avatar nkovacs commented on May 28, 2024 3

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.

from counterfeiter.

benmoss avatar benmoss commented on May 28, 2024 2

If you can make the interface private it won't add the interface assertion to the bottom of your fake

from counterfeiter.

nkovacs avatar nkovacs commented on May 28, 2024 1

Yes, the problem is that I cannot generate a fake for the artistDAO interface for use in the tests of the services package.

And yes, using a _test package works in this case, but I don't agree that having tests in the same package (i.e. white-box testing) is inherently a bad thing.

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 net/http and net/http/httptest as an example, but I don't think that's a really good example. net/http/httptest is useful for some special cases (testing http handlers and http servers), but if you look at the http package, its tests also have hand-crafted fakes, e.g.: https://github.com/golang/go/blob/master/src/net/http/fs_test.go#L620

from counterfeiter.

tjarratt avatar tjarratt commented on May 28, 2024

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.

from counterfeiter.

nkovacs avatar nkovacs commented on May 28, 2024

Here's an example of where I ran into this: https://github.com/qiangxue/golang-restful-starter-kit/blob/master/services/artist.go#L9 and https://github.com/qiangxue/golang-restful-starter-kit/blob/master/services/artist_test.go#L106

from counterfeiter.

ianaz avatar ianaz commented on May 28, 2024

Hi @nkovacs , I have the same issue. Are you still using your fork?

from counterfeiter.

subbuqq avatar subbuqq commented on May 28, 2024

i don't see the -pkg flag in the main build. is the fix not merged ?

from counterfeiter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.