Ask Your Question
2

In Googlemock for C++, is it possible to check all the components of an array pointer that is passed to a mocked function?

asked 2023-06-02 12:01:15 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-02 12:02:02 +0000

lakamha gravatar image

Yes, it is possible to check all the components of an array pointer that is passed to a mocked function using Googlemock for C++. A common way to do this is to use the ElementsAreArray() matcher, which allows the user to specify the expected array elements as a standard C++ array or a pointer followed by the number of elements.

Here's an example of how to use it:

// Suppose we have a mock function that takes an array of integers
MOCK_METHOD(void, myMockFunc, (int* arr, int arrSize));

// We can use EXPECT_CALL to set up the expectation
int expectedArr[] = {1, 2, 3};
EXPECT_CALL(mockObj, myMockFunc(ElementsAreArray(expectedArr)));

// Now we can call the function with an array and the expectation will be checked
int actualArr[] = {1, 2, 3};
mockObj.myMockFunc(actualArr, 3);

In this example, the ElementsAreArray() matcher ensures that the actualArr passed to myMockFunc() contains the same elements as expectedArr. If the expectation is not met, the test will fail.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-02 12:01:15 +0000

Seen: 16 times

Last updated: Jun 02 '23