googlemock

Matching arguments of custom type in googlemock

ぃ、小莉子 提交于 2019-12-10 13:49:08
问题 I have a problem matching a function argument to a specific object using google mock. Consider the following code: class Foo { public: struct Bar { int foobar; } void myMethod(const Bar& bar); } Now I have some testing code, it could look like this: Foo::Bar bar; EXPECT_CALL(fooMock, myMethod(Eq(bar)); So I want to make sure that when Foo::myMethod is called, the argument looks like my locally instantiated bar object. When I try this approach I get an error message like: gmock/gmock-matchers

Mocking an entire library

青春壹個敷衍的年華 提交于 2019-12-09 18:27:24
问题 I'm developing code that uses boost::asio . To test it, I need to mock a set of classes from this library. I'm using Google Mock, which allows for mocking virtual methods. The usual (and tedious) process would be to write an interface for each of the classes I need to use. On the other hand, the Google Mock Cookbook describes an alternative when it comes to mocking non-virtual methods: using templates. The problem in my case is that I might need to mock several classes at the same time (so

googlemock - mock a method that returns a complex datatyp

岁酱吖の 提交于 2019-12-08 19:12:11
问题 I want to mock a method that returns a complex datatyp class aClass { public: virtual const QMap<QString, QString> aMethod() const; } class MockaClass : public aClass { public: MOCK_CONST_METHOD0(aMethod, const QMap<QString, QString>()); } This code does not compile: "macro "MOCK_CONST_METHOD0" passed 3 arguments, but takes just 2" I think that the googlemock macro does not understand QMap and interpret the comma as parameter separator. Is there a way to tell googlemock that QMap is the

How to create a mock class with operator[]?

泄露秘密 提交于 2019-12-08 14:47:35
问题 I am having a class with operator[] , like this : class Base { public: virtual ~Base(){} virtual const int & operator[]( const unsigned int index ) const = 0; }; How can I create a mock class using google mock framework for this method? I tried this : class MockBase : public Base { public: MOCK_CONST_METHOD1( operator[], const int& ( const unsigned int ) ); }; but that produces next errors : error: pasting "]" and "_" does not give a valid preprocessing token error: pasting "]" and "_" does

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

五迷三道 提交于 2019-12-08 06:11:17
问题 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

Calling a method when expected method on mock was invoked

て烟熏妆下的殇ゞ 提交于 2019-12-07 12:52:04
问题 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) {}

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

核能气质少年 提交于 2019-12-07 08:55:25
问题 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

Google Mock: why NiceMock does not ignore unexpected calls?

柔情痞子 提交于 2019-12-07 07:56:37
问题 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,

Google Mock: “no appropriate default constructor available”?

吃可爱长大的小学妹 提交于 2019-12-07 00:14:00
问题 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

Google Mock: multiple expectations on same function with different parameters

 ̄綄美尐妖づ 提交于 2019-12-06 22:45:00
问题 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