Writing Good Unit Tests; Don't Mock Database Connections
qvault.io
qvault.io
This has many reasons why you'd want to do it. Many people stick to "performance" but I think that's the weakest reason. Ideally if you change some code and break something you want the method name of a failed test to tell you what's wrong. If you change some ORM code and 100% of your tests fail, is that helpful? Compared to the case where you change the ORM code and the unit tests testing the ORM code fail?
> you change some ORM code and 100% of your tests fail, is that helpful?
Yes. It shows what depends on the ORM behavior. Of course you need to be smart about finding the root cause.
It's possible I don't know Go well enough.
func (mdb *mockDB) Exec(query string, args ...interface{}) (sql.Result, error) { return nil, nil }
You're specifically returning nils, which is different than the native implementation from https://golang.org/pkg/database/sql/#DB.Exec
I would write a test using a mock and asserting the fact that it neither deals with an error nor does it return anything useful (no Result).
In this mock version, he's returning a nil for both. I take this as example code, because if the results are actually checked in the test, then the first result would have to be non-nil if the error was nil.
It doesn't really matter, because the author is saying not to do this.