DEV Community

Discussion on: Learn Go by writing tests: Dependency Injection

Collapse
 
quii profile image
Chris James

I see a lot of posts about hello world unit testing in Go but nothing really serious.

What's not serious about this? :)

And it's even worse with mgo.

I have used mgo. I would never write unit tests for mgo because it's someone else's code. Dont test other people's code. Test your interaction with it.

But how?

For those who dont know, mgo is a mongo driver for Go.

Forget about different classes of test or whatever. You want a bit of your code that can talk to the mongo db such as GetBankAccount(id string). Write a test for that. Inside it uses mgo, but that doesn't really matter, test the useful behaviour.

Mocking mgo gives you so little in terms of confidence your system works and if you decide you want to refactor to use a different library you suddenly have a bunch of useless tests.

Why no one talk about that ?

No one talks about it because it's too specific. You should be designing your software so that its as easy to test as the "not serious" examples you're reading.