gpt4 book ai didi

go - 验证模拟调用的顺序

转载 作者:行者123 更新时间:2023-12-01 22:11:35 24 4
gpt4 key购买 nike

我用 testify (v1.6.1) 并且需要测试如果接口(interface)的方法以正确的顺序调用。我查了documentation并试图在互联网上找到任何信息,但没有找到任何关于模拟订单检查的信息。
例子:

type InterfaceA interface {
Execute()
}

type InterfaceB interface {
Execute()
}
type Composition struct {
a InterfaceA
b InterfaceB
}

func (c * Composition) Apply() error {
//How to check that "a" execute before "b"?
c.a.Execute()
c.b.Execute()
return nil
}

最佳答案

即使存在 Unresolved 问题 (stretchr/testify/issue 741 "assert mock calls in order"),也不直接支持此功能
更普遍的问题 684 "Assert call order?"包括反驳:

IMO you should check the output of the function but not how it works internally. It may lead to testing implementation which is very hard to maintain.


虽然,在同一个线程中:

IMO there are cases to enforce order. i.e if you mock a mutex, you better check that Lock was always called before Unlock.
We can have a simple implementation where the mock has an "assertExpectationsInOrder" true/false flag that can be set before any expectations are added.


这可能会导致一些测试,例如 cassandra-operator/cmd/operator/controller_test.go 它记录事件以测试他们的订单。

关于go - 验证模拟调用的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63258154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com