I'm using mysql transaction in my project, I want to know how to write unit test when using db transaction? for example:
func SomeFunc() error {
return db.Transaction(func(tx *gorm.DB) {
// my business logic goes below
tx.Select(xxx)
tx.Create(xxx)
tx.Delete(xxx)
})
}
What's I think is that I rely on gorm strongly, that's may be bad. Do I need to define a trancation interface for only unit test?
one way to write a test when you have to test a database is always to bring up an in-memory database to test it or a real database with fake data I encourage you to write tests in this way not unit tests because in unit tests all your testing is the fake or dummy that you write here is an example of what I mean:
}