我有我繼承了一個項目的測試,看起來類似於此GTEST和GoogleMock EXPECT_CALL無法在Windows中,通過在Mac上使用char * PARAM
std::string value("test string");
const char * buffer = value.c_str();
EXPECT_CALL(object, foo(_,_,buffer, buffer.size(), _)).WillOnce(Return(0));
bar(value);
緩衝區是char *指向一個字串數據的。我已經插入了像對象這樣的虛擬值,只關注似乎在使用EXPECT_CALL的問題。在此EXPECT_CALL之後,將調用一個方法欄,它將原始字符串值作爲參數,然後在方法內調用帶有從原始字符串值構建的緩衝區的foo。
此測試正在研究此項目的Mac版本,但在Windows版本上失敗。它似乎在比較兩個字符指針的指針地址,即預期的和實際的,然後因爲它們不同而失敗。 foo方法絕對是在bar內調用的。
如果此測試方法(EXPECT_CALL)比較指針地址而不是該指針處的數據,那麼Mac上的測試也不應該失敗?
有人熟悉使用EXPECT_CALL和指針時Mac和Windows之間的明顯區別嗎?我看到
unknown file: error:
Unexpected mock function call - returning default value.
Function call: foo(NULL, 1, 0000000001CAAE78 pointing to "test string", 11,_)
Returns: 0
Google Mock tried the following 1 expectation, but it didn't match:
test.cpp(235): EXPECT_CALL(object, foo(_,_,buffer,buffer.size(),_)...
Expected arg #2: is equal to 0000000001CAAF78 pointing to "test string"
Actual: 0000000001CAAE78 pointing to "test string"
Expected: to be called once
Actual: never called - unsatisfied and active
test.cpp(235): error: Actual function call count doesn't match EXPECT_CALL(object, foo(_,_,buffer, buffer.size(), _)...
Expected: to be called once
錯誤我修改這個錯誤正好能體現我的例子。
非常感謝您的幫助。
'char * buffer = value.c_str();'不應該編譯。 – chris 2014-08-28 17:03:14
糾正爲const char * buffer = value.c_str()。意味着更多的僞代碼比完整的代碼抱歉的錯誤。 – astaubin 2014-08-28 17:31:10
不確定關於Mac/Windows與EXPECT_CALL的區別。也可能是不同的編譯器如何處理常量字符串。無論如何,如果它是你感興趣的字符串的值,那麼最好使用'foo(_,_,testing :: StrEq(buffer),... ...(它可以在Windows上運行)。 – SleuthEye 2014-08-29 01:26:23