简体   繁体   中英

GTest and GoogleMock EXPECT_CALL Fails in windows, passes on Mac with char * param

I have a test in a project I have inherited that looks similar to this

std::string value("test string");
const char * buffer = value.c_str();
EXPECT_CALL(object, foo(_,_,buffer, buffer.size(), _)).WillOnce(Return(0));
bar(value);

The buffer is a char * pointing to a string of data. I have inserted dummy values like object just to focus on the issue which seems to be in the use of EXPECT_CALL. Right after this EXPECT_CALL a method bar is called that takes the original string value as a parameter and then within the method calls foo with a buffer built from the original string value.

This test is working on the Mac build of this project, but failing on the Windows version. It seems to be comparing the pointer addresses for the two char pointers, the expected and the actual, and then failing because they are different. The method foo is definitely called within bar.

If this test method (EXPECT_CALL) compares the pointers addresses and not the data at that pointer then shouldn't the test fail on Mac as well?

Is anyone familiar with a distinct difference between Mac and windows when using EXPECT_CALL and pointers?

Error I am seeing

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

I modified this error just to reflect my example.

Thank you in advance for your help.

There doesn't seem to be any obvious differences between Mac & Windows with respect to EXPECT_CALL. I suppose there might also be differences between string implementation, and how compilers treat constant strings, which could explain the difference in behaviour.

However, I'd expect pointer arguments to be matched by address comparison. To compare values, you should be using specific Matchers . In particular for your case, there are a variety of String Matchers to choose from, including StrEq for string equality, which you may use as:

EXPECT_CALL(object, foo(_,_,testing::StrEq(buffer),value.size(),_))
  .WillOnce(Return(0));

The reason this works in Windows is because MSVC implements string pooling ( /GF ). See here:

http://msdn.microsoft.com/en-us/library/s0s0asdt.aspx

So why does this make it work? Because gmock is comparing pointer addresses, and when you have 2 identical constant strings the string pooling will cause them to have the same value. Turn this compiler option off and watch it fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM