2017-05-30 32 views
1

我有EXPECT_CALL(MockObj, func("abc")).Times(1)MockObjNiceMock谷歌模擬 - EXPECT_CALL觸發,即使功能有不同的觀點

在我的功能測試,有一個叫MockObj.func("def")除了MockObj.func("abc")

我會想到的是,合理的事情是谷歌模擬說

oh look we call func("def") but the arguments do not match the EXPECT_CALL ; nothing to see here

取而代之的是「抱怨」:

unknown file: Failure

Unexpected mock function call - taking default action specified at:

C:/work/unit_test.cpp:36:

Function call: func(84bf3d9 pointing to "def") 

     Returns: 1 

Google Mock tried the following 1 expectation, but it didn't match:

unit_test.cpp:50: EXPECT_CALL(MockObj, func("abc"))...

Expected arg #0: is equal to 84c8b96 pointing to "abc"

Actual: 84bf479 pointing to "def"

Expected: to be called once

Actual: called once - saturated and active

首先注意的期望仍然滿意,因爲MockObj.func("abc")被稱爲

我明白爲什麼GMock拋出了這個錯誤:我宣佈對func的期望,所以它試圖匹配cal l至func達到預期但與參數不匹配錯誤

很好。

但爲什麼GMock拋出錯誤?由於參數不匹配,爲什麼這種行爲選擇即

throw an error if the function matches existing EXPECT_CALL but not the arguments

回答

1

直接回答你的問題「但是爲什麼GMock拋出一個錯誤?」是:因爲它就是這樣設計的。

AFAIU googlemock - 你的情況googlemock以這種方式工作:

  1. EXPECT_CALL(MockObj, func("abc")); - 意思是: 「我很感興趣,在我的測試中Obj::func
  2. MockObjNiceMock的意思是:「我不關心的Obj任何其他的功能不EXPECT_CALL的」

如此明確主題 - 如果你的代碼下測試呼叫func("def") - 然後GOOGL電子模擬做的是:

  1. 我們感興趣的是func - 讓我們檢查
  2. 我們並不指望「高清」 - 所以測試失敗

也許你知道,你應該這樣做:

EXPECT_CALL(MockObj, func(StrNe("abc"))).Times(AnyNumber()); // meaning - do not care EXPECT_CALL(MockObj, func("abc")); // TImes(1) is default - you might skip it