gmock

Can a mock class inherit from another mock class in googlemock?

杀马特。学长 韩版系。学妹 提交于 2019-12-06 00:44:50
问题 Can a mock class inherit from another mock class in googlemock? If yes, then please help me in understanding why isn't this working. class IA { public: virtual int test1(int a) = 0; }; class IB : public IA { public: virtual float test2(float b) = 0; }; class MockA : public IA { public: MOCK_METHOD1(test1, int (int a)); }; class MockB : public MockA, public IB { public: MOCK_METHOD1(test2, float (float b)); }; I get a cannot instantiate abstract class compiler error for MockB but not for MockA

GMOCK - how to modify the method arguments when return type is void

我怕爱的太早我们不能终老 提交于 2019-12-05 21:41:01
I have a class which accepts a pointer to another class and has a method read(): class B: { public: ...... void read(char * str); ...... }; class A { public: A(B *bobj):b(bobj); B* b; void read (char * str); .......... }; I invoke the object like below A * aobj = new A(new B()); Now I should be able to access read method of both the classes like below: char text[100]; b->read(text) aobj->read(text) The method read of both A and B class is coded to copy some values to the input array as provided. How can I write MOCK method of the function to modify the argument to a specific value? ON_CALL(*b,

What is the easiest way to invoke a member function on an argument passed to a mocked function?

╄→гoц情女王★ 提交于 2019-12-05 21:31:31
Given the interfaces class IFooable { virtual void Fooable() = 0; }; class IFoo { virtual void Foo(IFooable* pFooable) = 0; }; and the goole mock mock class TMockFoo : public IFoo { MOCK_METHOD1(Foo, void (IFooable*)); }; what is the easiest way to specify an action which calls Fooable() on the argument to the mocked method Foo() ? I have tried TMockFoo MockFoo; ON_CALL(MockFoo, Foo(_)) .WithArg<0>(Invoke(&IFooable::Fooable)); but this doesn't compile because Invoke() with one argument expects a free function, not a member function. Using boost::bind should probably work, but won't necessarily

Google Mock: Is it ok to use global mock objects?

早过忘川 提交于 2019-12-05 18:17:07
In all the documentation about gmock I always find the mock object to be instantiated inside a test, like that: TEST(Bim, Bam) { MyMockClass myMockObj; EXPECT_CALL(MyMockObj, foo(_)); ... } So, the object is created and destroyed per test. I believe it's also perfectly fine to create and destroy the object per test fixture . But I'm wondering if it's also ok to have a file-global instance of the mock object, like that: MyMockClass myMockObj; TEST(Bim, Bam) { EXPECT_CALL(MyMockObj, foo(_)) ... } I tried it and I have absolutely no problems so far, it all seems to work fine. But maybe I should

How to use gmock to test that a class calls it's base class' methods

筅森魡賤 提交于 2019-12-05 08:22:14
class Foo { public: int x; int y; void move(void); }; class SuperFoo: public Foo { public: int age; void update(); }; SuperFoo::update(void) { move(); age++; } I'm just starting out with C++ and unit testing, I have some code resembling the above and I want to use gmock to test that SuperFoo::update() calls the base class' move() method. What would be that best way to attack this type of situation? One way is to make the move method virtual, and create a mock of your class: #include "gtest/gtest.h" #include "gmock/gmock.h" class Foo { public: int x; int y; virtual void move(void); //^^^^

gmock multiple in-out parameters SetArgReferee

匆匆过客 提交于 2019-12-04 23:59:42
I have an interface Itest: class Itest { bool testfunction(vector<int>& v, int& id); } I can mock it with: MOCK_METHOD2(testfunction, bool(vector<int>&, int&)) but how can I set the return values? I tried: vector<int> v; int i; EXPECT_CALL(testobject, testfunction(_,_, _)) .WillOnce(testing::SetArgReferee<0>(v)) .WillOnce(testing::SetArgReferee<1>(i)) .WillOnce(Return(true)); but then it is called three times.. How do I set these argReferees and the return value one time? VladLosev You combine several actions together using the DoAll action: EXPECT_CALL(testobject, testfunction(_, _, _))

Compare Eigen matrices in Google Test or Google Mock

家住魔仙堡 提交于 2019-12-04 21:14:23
问题 I was wondering if there is a good way to test two Eigen matrices for approximate equality using Google Test, or Google Mock. Take the following test-case as a simplified example: I am multiplying two complex valued matrices A , and B , and expect a certain result C_expect . I calculate the numerical result C_actual = A * B , using Eigen. Now, I want to compare C_expect , and C_actual . Right now, the corresponding code looks like this: #include <complex> #include <Eigen/Dense> #include

Compare Eigen matrices in Google Test or Google Mock

杀马特。学长 韩版系。学妹 提交于 2019-12-03 13:45:31
I was wondering if there is a good way to test two Eigen matrices for approximate equality using Google Test , or Google Mock . Take the following test-case as a simplified example: I am multiplying two complex valued matrices A , and B , and expect a certain result C_expect . I calculate the numerical result C_actual = A * B , using Eigen. Now, I want to compare C_expect , and C_actual . Right now, the corresponding code looks like this: #include <complex> #include <Eigen/Dense> #include <gtest/gtest.h> #include <gmock/gmock.h> typedef std::complex<double> Complex; typedef Eigen::Matrix2cd

incomplete type for std::any when GMOCKing interface

孤街醉人 提交于 2019-12-02 04:38:07
I have a very weird compilation problem with this snippet: #include <any> #include <gmock/gmock.h> struct Class { virtual std::any get(int, int) = 0; }; struct MockClass: Class { MOCK_METHOD2(get, std::any(int, int)); }; int foo() { MockClass dd; } Error gcc 9.1.0: /usr/include/c++/9.1.0/type_traits:131:12: error: incomplete type ‘std::is_copy_constructible<testing::internal::ReferenceOrValueWrapper<std::any> >’ used in nested name specifier clang 8.0.0: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/9.1.0/../../../../include/c++/9.1.0/type_traits:132:31: error: no member named 'value' in 'std::is

Google mock ByRef method

对着背影说爱祢 提交于 2019-12-01 02:59:27
I have a class that takes a boolean as a reference parameter and returns an integer: class Foo { public: Bar my_bar; virtual int myMethod(bool &my_boolean) = 0; } /*...*/ int Foo::myMethod(bool &my_boolean){ if (my_bar == NULL){ my_boolean = false; return -1; } else{ my_boolean = true; return 0; } } And I created a mock for this class: class MockFoo : public Foo { MOCK_METHOD1(myMethod,int(bool &my_boolean)); } I'm having problems on how to set the expectations for this kind of function,because I need to set the return value and the reference parameter to specific values to properly create my