我如何在 Go 中模拟 Stripe?

mry*_*yan 5 go stripe-payments testify

我正在尝试模拟 Stripe 进行一些测试。

//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)
}
Run Code Online (Sandbox Code Playgroud)

然后在测试初始化​​中:

//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,
})
Run Code Online (Sandbox Code Playgroud)

这可行 - 但我不知道如何模拟才能真正获得客户?我不想嘲笑client.API。API代码:https://github.com/stripe/stripe-go/blob/9c5fd87e31fd4a072b4d92571d67437e329dc9db/customer/client.go#L23

还有其他人这样做吗?:)

谢谢

koz*_*zmo 0

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

\n

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

\n
import (\n    "bytes"\n    "github.com/stretchr/testify/mock"\n    "github.com/stripe/stripe-go/v71"\n    "github.com/stripe/stripe-go/v71/form"\n)\n\ntype Backend struct {\n    mock.Mock\n}\n\nfunc (s *Backend) Call(method, path, key string, params stripe.ParamsContainer, v stripe.LastResponseSetter) error {\n    args := s.Called(method, path, key, params, v)\n    return args.Error(0)\n}\n\nfunc (s *Backend) CallRaw(method, path, key string, body *form.Values, params *stripe.Params, v stripe.LastResponseSetter) error {\n    args := s.Called(method, path, key, body, params, v)\n    return args.Error(0)\n}\n\nfunc (s *Backend) CallMultipart(method, path, key, boundary string, body *bytes.Buffer, params *stripe.Params, v stripe.LastResponseSetter) error {\n    args := s.Called(method, path, key, boundary, body, params, v)\n    return args.Error(0)\n}\n\nfunc (s *Backend) SetMaxNetworkRetries(maxNetworkRetries int64) {\n    s.Called(maxNetworkRetries)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

\xd0\x90lso 你必须严格遵循方法签名。

\n