2

i started unit-test with gtest and gmock. I want to test the content of an EXPECT_CALL in one line. How to do it the right way?

My example code:

class MockClientSignalEventHandler : public ISignalFunctionFeatureEventHandler<Data, Id> { public: MockClientSignalEventHandler() = default; MOCK_METHOD(void, onSignalEvent, (const std::shared_ptr<Id> &id, const std::shared_ptr<Data> &data), (override)); }; 

Now i want to check it in EXPECT_CALL. How can i check shared_ptr in EXPECT_CALL?

The code now looks like this:

EXPECT_CALL(*mockEventHandler, onSignal(::testing::_, ::testing::_)).WillOnce(ReturnFromDetached(&cv)); feature->handleFeatureMessage(messageHeader, data); std::mutex mx; std::unique_lock<std::mutex> lock(mx); cv.wait_for(lock, std::chrono::seconds(5)); 

Instead of ::testing::_ i want to check the content of shared_ptr.

Thanky you for your help!

1 Answer 1

0

Use testing::Pointee to "dereference" the shared_ptr. As the documentation notes, it works not only with raw pointers but also smart pointers. Minimal example (godbolt):

#include <memory> #include <gtest/gtest.h> #include <gmock/gmock.h> using ::testing::Pointee; using ::testing::Eq; struct Mock { MOCK_METHOD(void, Func, (std::shared_ptr<int> x)); }; TEST(Foo, Bar) { Mock m; EXPECT_CALL(m, Func(Pointee(Eq(42)))); m.Func(std::make_shared<int>(42)); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.