dongtan2017 2018-10-08 07:22
浏览 69
已采纳

测试中的模拟方法

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.

  • 写回答

1条回答 默认 最新

  • duandou8120 2018-10-08 10:31
    关注

    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")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!