I have to use the PgxConnIface interface in my source code? #64
-
Hello everyone, can someone please clarify this, I am a bit confused. The docs state I expected that I can do something like this
But I get the following error
Coming from some other languages to Go, this has been super confusing, there doesn't seem to be a clear-cut solution to testing using Postgres as your database. Also is there support for |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hello @Nikola-Milovic! Thanks a lot for your question. Let me answer by quoting the article "Mocking Techniques for Go":
TL;DR: We cannot mock Now, I see you are struggling to understand how to use such kind of interface in your code. You don't need to use any interfaces defined by 1. If it's enough in your case you may use type AccountService struct {
db pgx.Tx
} 2. If you really need a connection or a pool instead, then you need to define your own interface because there is no available in type PgxConnIface interface {
pgx.Tx
Begin(context.Context) (pgx.Tx, error)
Close(ctx context.Context) error
Ping(context.Context) error
//add more methods if you need
}
type AccountService struct {
db PgxConnIface
}
func main() {
accsrv := AccountService{}
accsrv.db, err := pgx.Connect(context.Background(), "postgres://rolname@hostname/dbname")
...
} Please pay attention to how examples are implemented. If you are comparing how the mocking library for |
Beta Was this translation helpful? Give feedback.
Hello @Nikola-Milovic!
Thanks a lot for your question. Let me answer by quoting the article "Mocking Techniques for Go":