Last active
February 15, 2022 14:25
-
-
Save dixudx/958dbf425cb9041046a399f5bd7e8720 to your computer and use it in GitHub Desktop.
Mock Golang Tests (If we want to test methodA, while it will call methodB internally, how can we mock the tests?)
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 demo | |
import ( | |
"context" | |
) | |
type ProviderInterface interface { | |
MethodA(ctx context.Context, id int) error | |
MethodB(ctx context.Context, id int) error | |
MethodC(ctx context.Context, id int) error | |
} | |
type providerImpl struct { | |
} | |
func (impl *providerImpl) MethodA(ctx context.Context, id int) error { | |
// do something | |
// then call method B | |
return impl.MethodB(ctx, id) | |
} | |
func (impl *providerImpl) MethodB(ctx context.Context, id int) error { | |
// do something | |
return nil | |
} | |
func (impl *providerImpl) MethodC(ctx context.Context, id int) error { | |
// do something | |
return nil | |
} |
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 demo | |
import ( | |
"context" | |
"testing" | |
) | |
type mockProvider struct { | |
methodAFunc func(ctx context.Context, id int) error | |
methodBFunc func(ctx context.Context, id int) error | |
methodCFunc func(ctx context.Context, id int) error | |
} | |
func (mp *mockProvider) MethodA(ctx context.Context, id int) error { | |
if mp.methodAFunc != nil { | |
return mp.methodAFunc(ctx, id) | |
} | |
return nil | |
} | |
func (mp *mockProvider) MethodB(ctx context.Context, id int) error { | |
if mp.methodBFunc != nil { | |
return mp.methodBFunc(ctx, id) | |
} | |
return nil | |
} | |
func (mp *mockProvider) MethodC(ctx context.Context, id int) error { | |
if mp.methodCFunc != nil { | |
return mp.methodCFunc(ctx, id) | |
} | |
return nil | |
} | |
func TestMethodA(t *testing.T) { | |
// we're testing methodA, which will call methodB | |
actualImpl := &providerImpl{} | |
tests := []struct { | |
name string | |
mp ProviderInterface | |
}{ | |
{ | |
mp: &mockProvider{ | |
methodAFunc: actualImpl.MethodA, // we can use actual providerImpl | |
methodBFunc: func(ctx context.Context, id int) error { | |
// mock some tests | |
return nil | |
}, | |
methodCFunc: nil, | |
}, | |
}, | |
{ | |
mp: &mockProvider{ | |
methodAFunc: func(ctx context.Context, id int) error { | |
// mock some tests | |
return nil | |
}, | |
methodBFunc: func(ctx context.Context, id int) error { | |
// mock some tests | |
return nil | |
}, | |
methodCFunc: nil, | |
}, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
if err := tt.mp.MethodA(context.TODO(), 123); err != nil { | |
t.Errorf("MethodA: %v", err) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment