gmock

Google mock ByRef method

狂风中的少年 提交于 2019-11-30 23:43:45
问题 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

Teach Google-Test how to print Eigen Matrix

与世无争的帅哥 提交于 2019-11-30 12:50:30
问题 Introduction I am writing tests on Eigen matrices using Google's testing framework Google-Mock, as already discussed in another question. With the following code I was able to add a custom Matcher to match Eigen matrices to a given precision. MATCHER_P2(EigenApproxEqual, expect, prec, std::string(negation ? "isn't" : "is") + " approx equal to" + ::testing::PrintToString(expect) + "\nwith precision " + ::testing::PrintToString(prec)) { return arg.isApprox(expect, prec); } What this does is to

Teach Google-Test how to print Eigen Matrix

孤街浪徒 提交于 2019-11-30 03:53:26
Introduction I am writing tests on Eigen matrices using Google's testing framework Google-Mock, as already discussed in another question . With the following code I was able to add a custom Matcher to match Eigen matrices to a given precision. MATCHER_P2(EigenApproxEqual, expect, prec, std::string(negation ? "isn't" : "is") + " approx equal to" + ::testing::PrintToString(expect) + "\nwith precision " + ::testing::PrintToString(prec)) { return arg.isApprox(expect, prec); } What this does is to compare two Eigen matrices by their isApprox method , and if they don't match Google-Mock will print a

Dependency injection with unique_ptr to mock

☆樱花仙子☆ 提交于 2019-11-28 11:19:07
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 so good, this works fine. However, I have a problem when I want to unit test the code: namespace {

Mock non-virtual method C++ (gmock)

↘锁芯ラ 提交于 2019-11-28 07:41:20
I have class class CSumWnd : public CBaseWnd { private: bool MethodA() } Please can you help how to mock MethodA() without making virtual, I didn't understand the concept of hi-perf dependency injection beduin It means you will have to templatize your production code. Using your example: CSumWind class definition: class CSumWnd : public CBaseWnd { private: bool MethodA() }; Mocked CSumWnd class definition: class MockCSumWnd : public CBaseWnd { private: MOCK_METHOD(MethodA, bool()); }; Production class which have to be tested with mocked class CSumWind . Now it becomes templated to provide

how to set custom ref-variable in gmock

我与影子孤独终老i 提交于 2019-11-28 00:07:55
问题 I am using gmock in my project and I meet a problem to set a custom reference variable for a mock function. Suppose I have a class as following: class XXXClient { public: void QueryXXX(const Request&, Response&); }; class XXXRunner { public: void DoSomething(XXXClient&); }; There is a Client Class XXXRunner::DoSomething using XXXClient::QueryXXX, and I Want to mock XXXClient to test XXXRunner::DoSomething. The problem occurs that the second parameter of QueryXXX , that is 'Response', is not a

Can gmock be used for stubbing C functions?

谁都会走 提交于 2019-11-27 01:54:52
问题 I am new to gmock, so I want to know how can I stub simple C function called in a function under test for Unit Testing. Example: int func(int a) { boolean find; // Some code find = func_1(); return find; } I have searched about gmock and in my understanding gmock does not provide functionality to stub simple C functions, therefore I want to ask does gmock provides functionality to mock or stub func_1 ? If not how can I stub func_1 manually in my test code without changing source code? I am

Mock non-virtual method C++ (gmock)

眉间皱痕 提交于 2019-11-27 01:24:11
问题 I have class class CSumWnd : public CBaseWnd { private: bool MethodA() } Please can you help how to mock MethodA() without making virtual, I didn't understand the concept of hi-perf dependency injection 回答1: It means you will have to templatize your production code. Using your example: CSumWind class definition: class CSumWnd : public CBaseWnd { private: bool MethodA() }; Mocked CSumWnd class definition: class MockCSumWnd : public CBaseWnd { private: MOCK_METHOD(MethodA, bool()); };