测试中的模拟方法

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.

The problem is that you defined the isTokenValid() in your MockAdapter instead of your ApeGatewayAdapter. So, when you are trying to test it says that

scsGateway.isTokenValid undefined (type *ApiGateway has no field or method isTokenValid)

You need to change your architecture in your structs because you've created a method New() for the adaptaer (and it's okay) but the important thing here (and almost always in Go) is returning an interface!

isTokenValid() is defined for the external type (in your test case for the MockAdapter) so you cannot call it unless you change the definition of your types or you change the implementation of the method from MockAdapter to ApeGatewayAdapter.

I think the best solution should be removing the StubAdapter and working directly with the ApiGateway and define the method isTokenValid() for this types. Then your test should works removing the internal ApeGatewayAdapterand using only the MockAdapter to mock the apigateway and its method.

Your code should look like this:

gateway.go

package gateway

type ApeGatewayAdapter interface {
    isTokenValid() bool
}

type ApiGateway struct {
    username string
    password string
    auth     Auth
}

type Auth struct {
    token   string
    validTo int32
}

func (a ApiGateway) isTokenValid() bool {
    // TODO add your validations
    return true
}

func New(host string, scheme string, username string, password string) ApeGatewayAdapter {

    return ApiGateway{username, password, Auth{}}
}

gateway_test

package gateway

import (
    "fmt"
    "testing"
)

type MockAdapter struct {
}

func (m MockAdapter) isTokenValid() bool {
    return true
}


func TestApiGateway_GetDevice(t *testing.T) {

    gateway := MockAdapter{}
    if gateway.isTokenValid() != true {
        t.Error("Not true")
    }
}