Skip to content

Instantly share code, notes, and snippets.

@samaita
Last active August 21, 2019 05:21
Show Gist options
  • Save samaita/c409a5efb4895486842474ad2bedcfa2 to your computer and use it in GitHub Desktop.
Save samaita/c409a5efb4895486842474ad2bedcfa2 to your computer and use it in GitHub Desktop.
Sample usage on gomock, generate a mock with mockgen
// Code generated by MockGen. DO NOT EDIT.
// Source: mocking.go
// Package sample is a generated GoMock package.
package sample
import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockIUser is a mock of IUser interface
type MockIUser struct {
ctrl *gomock.Controller
recorder *MockIUserMockRecorder
}
// MockIUserMockRecorder is the mock recorder for MockIUser
type MockIUserMockRecorder struct {
mock *MockIUser
}
// NewMockIUser creates a new mock instance
func NewMockIUser(ctrl *gomock.Controller) *MockIUser {
mock := &MockIUser{ctrl: ctrl}
mock.recorder = &MockIUserMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockIUser) EXPECT() *MockIUserMockRecorder {
return m.recorder
}
// GetUser mocks base method
func (m *MockIUser) GetUser(id int64) (UserSession, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetUser", id)
ret0, _ := ret[0].(UserSession)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetUser indicates an expected call of GetUser
func (mr *MockIUserMockRecorder) GetUser(id interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUser", reflect.TypeOf((*MockIUser)(nil).GetUser), id)
}
// MockITalk is a mock of ITalk interface
type MockITalk struct {
ctrl *gomock.Controller
recorder *MockITalkMockRecorder
}
// MockITalkMockRecorder is the mock recorder for MockITalk
type MockITalkMockRecorder struct {
mock *MockITalk
}
// NewMockITalk creates a new mock instance
func NewMockITalk(ctrl *gomock.Controller) *MockITalk {
mock := &MockITalk{ctrl: ctrl}
mock.recorder = &MockITalkMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockITalk) EXPECT() *MockITalkMockRecorder {
return m.recorder
}
// InsertTalk mocks base method
func (m *MockITalk) InsertTalk(uid int64, message string) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "InsertTalk", uid, message)
ret0, _ := ret[0].(error)
return ret0
}
// InsertTalk indicates an expected call of InsertTalk
func (mr *MockITalkMockRecorder) InsertTalk(uid, message interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertTalk", reflect.TypeOf((*MockITalk)(nil).InsertTalk), uid, message)
}
package sample
import (
"context"
"errors"
"log"
"time"
"github.com/tokopedia/kunyit/src/conf"
"github.com/tokopedia/sqlt"
)
var (
UserModule MockUser
TalkModule MockTalk
)
func InitController() {
UserModule = &UserController{}
TalkModule = &TalkController{}
}
type UserSession struct {
ID int64
Username string
Status int
}
type UserController struct{}
type MockUser interface {
GetUser(id int64) (UserSession, error)
}
func (u *UserController) GetUser(id int64) (UserSession, error) {
var user UserSession
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(conf.QueryTimeout)*time.Second)
defer cancel()
oauthClient := conf.DB.OauthClient
resp, err := oauthClient.GetBasicUserInfoClientCredentialsWithContext(ctx, id)
if err != nil {
log.Println(err)
return user, err
}
if resp.Error != "" {
err = errors.New(resp.Error)
return user, err
}
return user, nil
}
type TalkController struct{}
type MockTalk interface {
InsertTalk(uid int64, message string) error
}
func (t *TalkController) InsertTalk(uid int64, message string) error {
var err error
db, err := sqlt.Open("postgres", "http://127.0.0.1")
if err != nil {
return err
}
if err := db.Ping(); err != nil {
return err
}
query := `INSERT INTO ws_talk (id, message) VALUE ($1, $2)`
_, err = db.ExecContext(context.TODO(), query, uid, message)
if err != nil {
return err
}
return err
}
func CreateTalk(uid int64, message string) error {
var err error
var u UserSession
u, err = UserModule.GetUser(uid)
if err != nil {
return err
}
err = TalkModule.InsertTalk(u.ID, message)
if err != nil {
return err
}
return err
}
package sample
import (
"database/sql"
"testing"
gomock "github.com/golang/mock/gomock"
)
func TestCreateTalk(t *testing.T) {
type args struct {
uid int64
message string
errInsertTalk error
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "create talk success",
args: args{
uid: 100,
message: "hello world",
},
wantErr: false,
},
{
name: "create talk fail - db failure",
args: args{
uid: 100,
message: "hello world",
errInsertTalk: sql.ErrConnDone,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockUser := NewMockIUser(mockCtrl)
mockTalk := NewMockITalk(mockCtrl)
UserModule = mockUser
TalkModule = mockTalk
mockUser.EXPECT().GetUser(tt.args.uid).Return(UserSession{ID: tt.args.uid}, nil)
mockTalk.EXPECT().InsertTalk(tt.args.uid, tt.args.message).Return(tt.args.errInsertTalk)
if err := CreateTalk(tt.args.uid, tt.args.message); (err != nil) != tt.wantErr {
t.Errorf("CreateTalk() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
@samaita
Copy link
Author

samaita commented Aug 21, 2019

command

mockgen -source=sample.go -destination=mock.sample.go -package=sample github.com/samaita/sample mockUser,mockTalk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment