簡體   English   中英

當 function 被多次調用時,有沒有辦法每次調用 AssertCalled

[英]Is there a way to AssertCalled every call when a function is called multiple times

我正在嘗試使用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並驗證是否確實使用預期的 function 調用了data1data2 但我只能通過 function 的最后一次調用來斷言,即使用data2 有什么方法或如何我也可以用data1斷言調用?

文檔中的示例:

/*
  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任意次數來記錄任意數量的“以這種方式和這種方式調用”的期望。

我只是閱讀源代碼,真的。 打賭它會比在 SO 上發帖更快。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM