首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎么才能在Go中模仿Stripe呢?

我怎么才能在Go中模仿Stripe呢?
EN

Stack Overflow用户
提问于 2020-06-23 19:47:14
回答 1查看 557关注 0票数 0

我正在试着模拟一些测试的条纹。

代码语言:javascript
复制
//testify mock
type Backend struct {
    mock.Mock
}

func (s Backend) Call(method, path, key string, params stripe.ParamsContainer, v interface{}) error {
    args := s.Called(params)
    return args.Error(0)
}

func (s Backend) CallRaw(method, path, key string, body *form.Values, params *stripe.Params, v interface{}) error {
    args := s.Called(params)
    return args.Error(0)
}

func (s Backend) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *stripe.Params, v interface{}) error {
    args := s.Called(params)
    return args.Error(0)
}

func (s Backend) SetMaxNetworkRetries(maxNetworkRetries int) {
    s.Called(maxNetworkRetries)
}

然后在测试初始化中:

代码语言:javascript
复制
//stripe
backend := new(mock.Backend)
backend.On("CallRaw", testify.Anything, testify.Anything, testify.Anything, testify.Anything, testify.Anything).
    Return(nil)
backend.On("Call",
    testify.
        MatchedBy(func(req stripe.ParamsContainer) bool {
            customerParams, ok := req.(*stripe.CustomerParams)
            if ok {
                return *customerParams.Email == "failure@email.com"
            }
            return false
        })).
    Return(fmt.Errorf("downstream stripe error"))
backend.On("Call",
    testify.
        MatchedBy(func(req stripe.ParamsContainer) bool {
            customerParams, ok := req.(*stripe.CustomerParams)
            if ok {
                return *customerParams.Email == "success@email.com"
            }
            return false
        })).
    Return(nil)
sc := &client.API{}
sc.Init("", &stripe.Backends{
    API:     backend,
    Connect: backend,
    Uploads: backend,
})

这是可行的--但我想不出我该如何模仿才能真正赢得客户?我不想嘲笑client.API。接口代码:https://github.com/stripe/stripe-go/blob/9c5fd87e31fd4a072b4d92571d67437e329dc9db/customer/client.go#L23

还有别的人在做这个吗?:)

谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-06-23 22:47:07

我认为你错误地实现了这个接口。

我对github.com/stripe/stripe-go/v71 v71.27.0的认识

代码语言:javascript
复制
import (
    "bytes"
    "github.com/stretchr/testify/mock"
    "github.com/stripe/stripe-go/v71"
    "github.com/stripe/stripe-go/v71/form"
)

type Backend struct {
    mock.Mock
}

func (s *Backend) Call(method, path, key string, params stripe.ParamsContainer, v stripe.LastResponseSetter) error {
    args := s.Called(params)
    return args.Error(0)
}

func (s *Backend) CallRaw(method, path, key string, body *form.Values, params *stripe.Params, v stripe.LastResponseSetter) error {
    args := s.Called(params)
    return args.Error(0)
}

func (s *Backend) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *stripe.Params, v stripe.LastResponseSetter) error {
    args := s.Called(params)
    return args.Error(0)
}

func (s *Backend) SetMaxNetworkRetries(maxNetworkRetries int64) {
    s.Called(maxNetworkRetries)
}

要实现mock,您必须使用指针作为目标func (s *Backend) SetMaxNetworkRetries(maxNetworkRetries int64){...},而不是方法的复制目标(func (s Backend) SetMaxNetworkRetries(maxNetworkRetries int64){...})。

Аlso你必须严格遵循方法签名。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62533966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档