登录
首页 >  Golang >  Go问答

在实验中使用的模拟技术

来源:stackoverflow

时间:2024-03-01 20:12:26 333浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《在实验中使用的模拟技术》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我有一个我想测试的课程:

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 { ... }

从类内的所有其他方法调用此 istokenvalid 方法,以验证和/或更新 api 令牌。在测试中我想模拟这个方法,所以它总是返回 true。

我如何实现这一目标?

编辑: 我的代码:

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())
}

我希望测试调用我的模拟方法并返回true,但事实并非如此。


解决方案


问题是您在 mockadapter 而不是 apegatewayadapter 中定义了 istokenvalid()。因此,当您尝试测试时,它会显示

scsgateway.istokenvalid 未定义(类型 *apigateway 没有字段或 方法 istokenvalid)

你需要改变你的结构中的架构,因为你已经为适配器创建了一个方法 new() (这没问题),但是这里重要的事情(并且几乎总是在 go 中)是返回一个接口!

istokenvalid() 是为外部类型定义的(在 mockadapter 的测试用例中),因此除非更改类型的定义或将方法的实现从 mockadapter 更改为 apegatewayadapter,否则无法调用它。 p>

我认为最好的解决方案应该是删除 stubadapter 并直接使用 apigateway 并为此类型定义方法 istokenvalid() 。然后,您的测试应该可以仅使用 mockadapter 来模拟 apigateway 及其方法,从而删除内部 apegatewayadapterand。

您的代码应如下所示:

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")
    }
}

以上就是《在实验中使用的模拟技术》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>