我正在嘗試使用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驗證兩者確實是用預期的函數(shù)調用的。但是我只能在函數(shù)的最后一次調用中斷言,即使用. 有什么方法或我可以用它來斷言調用嗎?data1data2data2data1
1 回答

慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
文檔中的示例:
/*
Actual test functions
*/
// TestSomething is an example of how to use our test object to
// make assertions about some target code we are testing.
func TestSomething(t *testing.T) {
// create an instance of our test object
testObj := new(MyMockedObject)
// setup expectations
testObj.On("DoSomething", 123).Return(true, nil)
// call the code we are testing
targetFuncThatDoesSomethingWithObj(testObj)
// assert that the expectations were met
testObj.AssertExpectations(t)
}
看起來您可以調用.On任意次數(shù)來記錄任意數(shù)量的“以這種方式和這種方式調用”的期望。
我只是閱讀源代碼,真的。打賭它會比在 SO 上發(fā)帖更快。
- 1 回答
- 0 關注
- 162 瀏覽
添加回答
舉報
0/150
提交
取消