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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集