googlemock

Create gmock tests for template specialization methods

断了今生、忘了曾经 提交于 2021-01-29 19:56:07
问题 I want to add GMOCK tests to verify if the container accesses the correct method. For vector it should access the second method, and for set it should access the first method (because set has set.find ). This is my template specialization: namespace tools{ struct low_priority {}; struct high_priority : low_priority {}; template<class TSource, class Ty> auto exists_in(high_priority, const TSource &source, const Ty &item) -> decltype(source->find(item) != source.end()) { return source.find(item

I am doing unit testing using gtest and gmock frameworks and I need help in stubbing/mocking a external C functions used inside class functions

与世无争的帅哥 提交于 2021-01-21 10:22:37
问题 So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ? My production code : prod_code.cpp int TargetTestClass::targetFunc() { if(externalCFunc() == True) { statement1; statement2; statement3; /// and so on } } My testcode.cpp generally contains tests like this //Fixture for

I am doing unit testing using gtest and gmock frameworks and I need help in stubbing/mocking a external C functions used inside class functions

三世轮回 提交于 2021-01-21 10:21:44
问题 So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ? My production code : prod_code.cpp int TargetTestClass::targetFunc() { if(externalCFunc() == True) { statement1; statement2; statement3; /// and so on } } My testcode.cpp generally contains tests like this //Fixture for

How to Mock the return object

不想你离开。 提交于 2021-01-07 03:11:23
问题 I try to mock a User class and its nested struct UserBuilder: class User { public: virtual int loadData(const std::string& filename); virtual UserBuilder getUserBuilder(const std::string& functionName) const; struct UserBuilder { UserBuilder(std::string functionName) : m_functionName{functionName} {}; virtual ~UserBuilder(); virtual UserBuilder& fun1(); virtual UserBuilder& fun2(int32_t num); virtual bool callFunction(); private: std::string m_functionName{}; }; } This is the mock class for

How to Mock the return object

99封情书 提交于 2021-01-07 03:09:07
问题 I try to mock a User class and its nested struct UserBuilder: class User { public: virtual int loadData(const std::string& filename); virtual UserBuilder getUserBuilder(const std::string& functionName) const; struct UserBuilder { UserBuilder(std::string functionName) : m_functionName{functionName} {}; virtual ~UserBuilder(); virtual UserBuilder& fun1(); virtual UserBuilder& fun2(int32_t num); virtual bool callFunction(); private: std::string m_functionName{}; }; } This is the mock class for

How to Mock the return object

余生颓废 提交于 2021-01-07 03:08:57
问题 I try to mock a User class and its nested struct UserBuilder: class User { public: virtual int loadData(const std::string& filename); virtual UserBuilder getUserBuilder(const std::string& functionName) const; struct UserBuilder { UserBuilder(std::string functionName) : m_functionName{functionName} {}; virtual ~UserBuilder(); virtual UserBuilder& fun1(); virtual UserBuilder& fun2(int32_t num); virtual bool callFunction(); private: std::string m_functionName{}; }; } This is the mock class for

Mock static method from external Class (that I can't change!)

删除回忆录丶 提交于 2020-12-06 08:09:12
问题 I want to mock (with gmock) a static function from a class that I can't change. A is the class that I want to mock: Class A { public: static std::string get_id(); ... } B is my class that I want to test with gmock: Class B { public: B(A *a_ptr); ... std::string foo(); private: A *m_a_ptr; } B::B(A *a_ptr) : m_a_ptr(a_ptr) { } std::string B::foo() { id = m_a_ptr->get_id(); return id; } How can I mock the method get_id without changing the class A? 回答1: Static dependency injection and GMock

Googletest: How to run tests asynchronously?

耗尽温柔 提交于 2020-12-05 10:22:31
问题 Given a large project with thousands of tests, some of which take multiple minutes to complete. When executed sequentially, the whole set of test takes more than an hour to finish. The testing time could be reduced by executing tests in parallel. As far as I know there are no means to do that directly from googletest/mock, like a --async option. Or am I wrong? One solution is to determine the tests which can run in parallel and write a script that starts each in a separate job, i.e. ./test -

How to use gmock MOCK_METHOD for overloaded operators?

耗尽温柔 提交于 2020-07-20 07:26:22
问题 I am new to googlemock (and StackOverflow). I got a problem when using MOCK_METHODn in googlemock and I believe this function is widely used. Here is what I did. I have an abstract class Foo with virtual overloaded operator[] : class Foo{ public: virtual ~Foo(){}; virtual int operator [] (int index) = 0; } and I want to use googlemock to get a MockFoo : class MockFoo: public Foo{ public: MOCK_METHOD1(operator[], int(int index)); //The compiler indicates this line is incorrect } However, this

gmock/google-mock issues warning and fails the test with mocking exceptions

强颜欢笑 提交于 2020-06-17 09:45:52
问题 I have coded a demo mock using google mock. The issue is that it is failing and not properly mocking. I cannot understand the issue here. Code: test/mock_turtle_test.cc #include "mock_turtle.h" #include "../painter.h" #include <gtest/gtest.h> using ::testing::_; using ::testing::AtLeast; using ::testing::Return; ACTION(MyException) { throw(error_k()); } TEST(PainterTest, CanDrawSomething) { Painter painter; EXPECT_CALL(*(painter.getTurtle()), PenDown()) .Times(AtLeast(1)) .WillOnce