Last active
January 26, 2022 11:40
-
-
Save eftalyurtseven/d74d5d3e10ef973df26dc037fd8f6cf7 to your computer and use it in GitHub Desktop.
How to Mock Database with GoMock? - https://medium.com/@eftal/how-to-mock-database-with-gomock-9bd0a92ffc10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package order | |
import ( | |
"context" | |
"eftal/medium/mocks" | |
"errors" | |
"fmt" | |
"testing" | |
"github.com/golang/mock/gomock" | |
"github.com/jackc/pgx/v4" | |
) | |
func TestMakeAnOrder(t *testing.T) { | |
ctx := context.Background() | |
req := &orderRequest{ | |
TableNumber: "B-1", | |
Products: map[string]int{ | |
"Pizza": 5, | |
"Chefs' Special": 3, | |
"Coke": 2, | |
}, | |
} | |
ctrl := gomock.NewController(t) | |
defer ctrl.Finish() | |
pool := mocks.NewMockFakePoolInterface(ctrl) | |
dummyError := fmt.Errorf("db.BeginTx failed with err: connection failed") | |
pool.EXPECT().BeginTx(ctx, pgx.TxOptions{}).Return(nil, dummyError) | |
ordersvc := &OrderService{db: pool} | |
_, err := ordersvc.Make(ctx, req) | |
if !errors.Is(err, dummyError) { | |
t.Fatalf("erros doesn't matched!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment