Scaling Go Testing with Contract and Scenario Mocks

(funnelstory.ai)

20 points | by preetamjinka 6 days ago ago

5 comments

  • gethly 6 minutes ago ago

    Testing in Go is great, for unit testing. But anything above that should be done manually. "Mocking" never made sense to me and I never used it in my entire life. If I want to test a database/repository, I will spin up a real database with real data and data persistence instead of relying on in-memory storage and try to abstract away things that make no sense to do in the real world, as we're not living in a theoretical world of interfaces and adapters and whatnot.

  • SPascareli13 an hour ago ago

    I really dislike this idea of testing in go: only ever use an interface, never the real implementation + mockgen the mocks based on this interface + use the mocks to assert that a function is called, with exactly this parameters and in this exact order.

    I find this types of tests incredibly coupled with the implementation, since any chance require you to chance your interfaces + mocks + tests, also very brittle and many times it ends up not even testing the thing that actually matters.

    I try to make integration test whenever possible now, even if they are costly I find that the flexibility of being able to change my implementation and not break a thousand tests for no reason much better to work with.

    • tonyhb 37 minutes ago ago

      If you're testing the interface, changing the implementation internals won't create any churn (as the mocks and tests don't change).

      If you are changing the interface, though, that would mean a contract change. And if you're changing the contract, surely you wouldn't be able to even use the old tests?

      This isn't really a go problem at all. Any contract change means changing tests.

    • esafak an hour ago ago

      Don't you mean testing the interface of the implementation? I see nothing wrong with that, if so.

  • teeray 2 hours ago ago

    > Mocks are static, but reality evolves.

    I learned “test your mocks” long ago from Sandi Metz, and that advice has paid off well for me. Have some set of behavioral conformance tests for the kind of thing you expect (e.g. any database worth its salt should be able write and read back the same record). Then stick your mock right under that same battery of tests alongside your implementation(s). If either deviate from the behavior you depend on, you’ll know about it.