googlemock

GoogleMock: how to expect precisely one call with a certain argument, and see diagnostic on failure?

纵饮孤独 提交于 2019-12-06 20:45:29
Maybe a finesse question, my problem is that if I write: EXPECT_CALL(mock, handleMessage(_)).Times(0); // expectation #1 EXPECT_CALL(mock, handleMessage(Pointee(IsLike(aSpecificMessage)))); // expectation #2 ... and method handleMessage is called once, but with a different argument (not aSpecificMessage ), then the failure looks like: Mock function called more times than expected - returning default value. Function call: handleMessage(0x8b5378) Returns: false Expected: to be never called Actual: called once - over-saturated and active Google Mock doesn't print the diagnostic on why the

Can googlemock mock method calls from within other method calls of the same class?

断了今生、忘了曾经 提交于 2019-12-06 09:42:10
Is it possible to mock method calls from within other method calls of the same class? I am new to C++ (primarily a C developer) and very new to googlemock and Google Test so forgive me if this is answered elsewhere and I didn't understand the answer! Below is a simple example that should explain what I want to do. Using the example below, I want to mock ReturnInput , while testing ReturnInputPlus1 . using ::testing::Invoke; using ::testing::_; using ::testing::Return; class MyClass { public: MyClass() : x(1) {} virtual ~MyClass() {} int ReturnInput(int x) { return x; } int ReturnInputPlus1(int

C++ High performance unit testing with Google Mock?

被刻印的时光 ゝ 提交于 2019-12-06 04:01:15
问题 I'm using Google Mock, and I'm struggling to mock out C++ system calls (specifically the C++11 chrono functions). I'm know I'm supposed to make an interface, create a class to implement the interface for my actual implementation, and then mock out the interface on my tests. I'm trying to write an embedded application, so this level of indirection sounds too expensive for me. What is the most efficient/performant way to incorporate system calls into Google Mock? 回答1: No, you don't need to

Calling a method when expected method on mock was invoked

大城市里の小女人 提交于 2019-12-05 18:34:39
I have the following scenario: class InterfaceA; class InterfaceB; class InterfaceC; class InterfaceA { virtual void foo(InterfaceC&) = 0; }; class InterfaceB { virtual void bar() = 0; }; class InterfaceC { virtual void bla() = 0; }; // MOCKs class MockA : public InterfaceA { public: MOCK_METHOD0(foo, void(InterfaceC&)); }; class MockB : public InterfaceB { public: MOCK_METHOD0(bar, void()); }; class ImplC : public InterfaceC { public: ImplC(InterfaceA& a, Interface& b) : m_a(a), m_b(b) {} void doSomething() { m_a.foo(*this); } virtual void bla() { m_b.bar(); } }; MockA mockA; MockB mockB;

Why does Google Test/Mock show leaked mock object error by std::unique_ptr?

蓝咒 提交于 2019-12-05 18:16:44
Let's assume that there is Bar object which uses a Foo object. The ownership is exclusive, so Bar gets Foo as a std::unique_ptr in its constructor. I would like to test Bar with Google Test framework, so I made a following code: using namespace testing; class Foo { public: virtual int F() = 0; }; class Bar { public: Bar(std::unique_ptr<Foo>&& foo) : m_foo(std::move(foo)) { } int B() { return m_foo->F(); } private: std::unique_ptr<Foo> m_foo; }; class MockFoo : public Foo { public: MOCK_METHOD0(F, int()); }; class BarTest : public Test { }; TEST_F(BarTest, Test1) { auto mock_foo = std::make

How to set a value to void * argument of a mock method in google mock testing?

只谈情不闲聊 提交于 2019-12-05 17:01:53
I want to pass a string "Device Name" to a void * pointer argument of a method and retrieve it to a character array later. For this I've done as shown below. Here I have created an action to achieve this. ACTION_P(SetArg2ToChar, value) {*static_cast<char*>(arg2) = *value; } Actual method to be called/mocked bool getDictItem(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo) My mock method MOCK_METHOD5(getDictItem, bool(WORD wIndex, BYTE bSubIndex, void * pObjData, DWORD dwLength, CSdo& sdo)); in code it is called as if( !can.getDictItem(wIndex, bSubIndex, pObjData,

Google Mock: why NiceMock does not ignore unexpected calls?

北城以北 提交于 2019-12-05 12:27:33
I am using Google Mock 1.7.0 with Google Test 1.7.0. The problem is when I use NiceMock I get test failures because of unexpected mock function call (which should be ignored by NiceMock as per Google Mock documentation). The code looks like this: // Google Mock test #include <gtest/gtest.h> #include <gmock/gmock.h> using ::testing::Return; using ::testing::_; class TestMock { public: TestMock() { ON_CALL(*this, command(_)).WillByDefault(Return("-ERR Not Understood\r\n")); ON_CALL(*this, command("QUIT")).WillByDefault(Return("+OK Bye\r\n")); } MOCK_METHOD1(command, std::string(const std::string

GoogleMock and GoogleTest in Visual Studio 2010

廉价感情. 提交于 2019-12-05 09:13:57
问题 Has anyone successfully built gmock and gtest in Visual Studio 2010? I've tried with version 1.5.0, but I only get incomprehensible compilation errors. 回答1: I found this thread in google groups about issues found when building gmock-1.5.0 under VS2010. Following the thread, I've created a short readme file, which worked for me, so here it is: Download gmock 1.5.0 from Google Mock. Extract to library folder on the machine (e.g. C:\Libs\gmock-1.5.0). From now on, this folder will be reffered as

Google Mock: “no appropriate default constructor available”?

风流意气都作罢 提交于 2019-12-05 05:27:40
Using Visual Studio 2010 C++ with googlemock. I'm trying to use a mock I created and I'm getting the compiler error on the line: EmployeeFake employeeStub; The error is: 1>c:\someclasstests.cpp(22): error C2512: 'MyNamespace::EmployeeFake' : no appropriate default constructor available EmployeeFake: class EmployeeFake: public Employee{ public: MOCK_CONST_METHOD0(GetSalary, double()); } Employee: class Employee { public: Employee(PensionPlan *pensionPlan, const char * fullName); virtual ~Employee(void); virtual double GetSalary() const; } I gather that the problem is that the base class doesn't

Google Mock: multiple expectations on same function with different parameters

99封情书 提交于 2019-12-05 04:02:33
Consider the case where a certain mocked function is expected to be called several times, each time with a different value in a certain parameter. I would like to validate that the function was indeed called once and only once per value in a certain list of values (e.g. 1,2,5). On the other hand, I would like to refrain from defining a sequence as that would dictate a certain order, which is an implementation detail I would like to keep free. Is there some kind of matcher, or other solution for this case? I'm not sure if this influences the solution in any way but I do intend to use WillOnce