I have a class i want to test:
type ApiGateway struct {
username string
password string
scsClient *scsClient.APIDocumentation
auth Auth
}
type Auth struct {
token string
validTo int32
}
func New(host string, scheme string, username string, password string) *ApiGateway {
var config = scsClient.TransportConfig{host, "/", []string{scheme}}
var client = scsClient.NewHTTPClientWithConfig(strfmt.Default, &config)
return &ApiGateway{username, password, client, Auth{}}
}
func (s *ApiGateway) isTokenValid() bool { ... }
This isTokenValid method is called from every other method inside the class in order to verify and/or update the API token. In tests I want to mock this method, so it always returns true for example.
How do I archieve that?
Edit: My Code:
apiGateway.go
type StubAdapter struct {
ApeGatewayAdapter
}
type ApeGatewayAdapter interface {
isTokenValid() bool
}
type ApiGateway struct {
username string
password string
scsClient *scsClient.APIDocumentation
auth Auth
adapter ApeGatewayAdapter
}
type Auth struct {
token string
validTo int32
}
func New(host string, scheme string, username string, password string, a ApeGatewayAdapter) *ApiGateway {
var config = scsClient.TransportConfig{host, "/", []string{scheme}}
var client = scsClient.NewHTTPClientWithConfig(strfmt.Default, &config)
return &ApiGateway{username, password, client, Auth{}, a}
}
apiGateway_test.go
type MockAdapter struct {
ApeGatewayAdapter
}
func (m MockAdapter) isTokenValid() bool {
return true
}
func TestApiGateway_GetDevice(t *testing.T) {
var scsGateway = New("foo.com", "http", "testuser", "testpwd", MockAdapter{})
fmt.Println(scsGateway.isTokenValid())
}
I would expect the test to call my mocked method and return true which it doesnt.