我正在尝试使用stretchr/testify对如下代码进行单元测试:
func (c *MyClient) upsertData(data MyObject) {
upsertToDatabase(data)
}
func doSomething(c *MyClient) {
data1, data2 := getSomeData()
c.upsertToDatabase(data1)
c.upsertToDatabase(data2)
}
// Unit test.
func TestDoSomething(t *testing.T) {
c := mock.MyClient{}
doSomething(c)
/* The following line checking for data1 upsert failed.
* require.True(t, c.AssertCalled(t, "upsertToDatabase", mock.MatchedBy(func(data MyObject) bool { return data == MyObject{expectedObject1 /* data2 */}})) */
require.True(t, c.AssertCalled(t, "upsertToDatabase", mock.MatchedBy(func(data MyObject) bool { return data == MyObject{expectedObject1 /* data2 */}}))
}
我想调用并AssertCalled验证两者确实是用预期的函数调用的。但是我只能在函数的最后一次调用中断言,即使用. 有什么方法或我可以用它来断言调用吗?data1data2data2data1
慕无忌1623718
相关分类