gmock

incomplete type for std::any when GMOCKing interface

落爺英雄遲暮 提交于 2020-01-11 12:35:35
问题 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

Dependency injection with unique_ptr to mock

霸气de小男生 提交于 2019-12-28 17:55:37
问题 I have a class Foo that uses class Bar. Bar is used only in Foo and Foo is managing Bar, therefore I use unique_ptr (not a reference, because I don't need Bar outside of Foo): using namespace std; struct IBar { virtual ~IBar() = default; virtual void DoSth() = 0; }; struct Bar : public IBar { void DoSth() override { cout <<"Bar is doing sth" << endl;}; }; struct Foo { Foo(unique_ptr<IBar> bar) : bar_(std::move(bar)) {} void DoIt() { bar_->DoSth(); } private: unique_ptr<IBar> bar_; }; So far

Dependency injection with unique_ptr to mock

大城市里の小女人 提交于 2019-12-28 17:55:29
问题 I have a class Foo that uses class Bar. Bar is used only in Foo and Foo is managing Bar, therefore I use unique_ptr (not a reference, because I don't need Bar outside of Foo): using namespace std; struct IBar { virtual ~IBar() = default; virtual void DoSth() = 0; }; struct Bar : public IBar { void DoSth() override { cout <<"Bar is doing sth" << endl;}; }; struct Foo { Foo(unique_ptr<IBar> bar) : bar_(std::move(bar)) {} void DoIt() { bar_->DoSth(); } private: unique_ptr<IBar> bar_; }; So far

GMock and mocking constructors in a class with non virtual methods

巧了我就是萌 提交于 2019-12-24 02:33:15
问题 i need to mock a class that has only non virtual methods. This class has a copy constructor. How to I write a mock method for that. I get a compiler error if I just use the MOCK_METHOD1(classname, void(classname& source)); Thanks in advance. 回答1: You can't mock non-virtual functions with gmock. So the first alternative to consider is to make the functions virtual. If you are concerned with performance overhead of making the functions virtual make sure that this really is a problem (by

EXPECT_CALL without mock in Google Test

人盡茶涼 提交于 2019-12-24 02:10:04
问题 Is there any way to test a function call via GoogleTest for c++ without creating mock object, e.g. we have the following production code: if (a) method(x); I would like to test whether the method will be called in the case a is True and a is False. I would like to construct a test that does exactly the same what Google Test's EXPECT_CALL does, but EXPECT_CALL works only with the mock object's method. In my case I would prefer not to use a mock (there is no need to create any object). 回答1: As

How to unit test the std::bind function using gtest?

家住魔仙堡 提交于 2019-12-23 17:33:54
问题 I am trying to write unittest cases for some of the cpp files in my project. The scenario here is: I have a cpp file with only one public method defined and in turn which calls private methods. Here the private methods are called in the public method as a callback method. How do I test the private methods here. I will be doing the mocking for Callback pointer and I am not sure how to call the private method. Please give me some suggestions how to call the private methods in this scenario

Testing with GTest and GMock: shared vs. static libraries

南楼画角 提交于 2019-12-23 10:00:06
问题 I think this question may violate some of the Q&A standards for the site, as the answer(s) I may receive could be regarded as opinion-driven. Nevertheless, here it goes... Suppose we're working on a C++ project, using CMake to drive the build/testing/packaging process, and GTest and GMock for testing. Further suppose the structure of our project looks like this: cool_project | |-- source | | | |-- module_foo | | | | | |-- (bunch of source files) | | | |-- module_bar | | | |-- (yet more source

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

拟墨画扇 提交于 2019-12-22 10:27:51
问题 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

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

◇◆丶佛笑我妖孽 提交于 2019-12-22 05:22:04
问题 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? 回答1: One way is to make the move method virtual, and create a mock of your class: #include

gmock multiple in-out parameters SetArgReferee

℡╲_俬逩灬. 提交于 2019-12-22 01:26:08
问题 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? 回答1: You combine