googlemock

Google Mock: Mock private variable member that is instantiated in target class's constructor

不问归期 提交于 2019-12-24 08:33:51
问题 My question is the same as Mockito: Mock private field initialization but for Google Mock framework. In a nutshell: class Target { private: Person person = new Person(); public: void testMethod() { person.someMethod(); } }; How can I mock the person instance while making unit tests for Target class? 回答1: A non-answer here: simply don't do it this way. Your problem is the call to new here. Thing is: that makes testing hard, and it also creates a very tight coupling between the Target and the

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

Using googlemock with fake impls of non-virtual functions

倾然丶 夕夏残阳落幕 提交于 2019-12-22 16:35:42
问题 I've got legacy code with some unit tests based on googlemock framework. While I was trying to extend the unit tests with some new scenarios I met the following problem: class D { public: void pubMethod1(); int pubMethod2(); // There are pretty much non-virtual methods, both public and private ... protected: uint method3(); void method4(); ... // Some class members are here }; class SUT { public: ... protected: D _dep; }; The SUT class (software under test) should be tested, its

Can googlemock mock method calls from within other method calls of the same class?

ぐ巨炮叔叔 提交于 2019-12-22 11:36:41
问题 Is it possible to mock method calls from within other method calls of the same class? I am new to C++ (primarily a C developer) and very new to googlemock and Google Test so forgive me if this is answered elsewhere and I didn't understand the answer! Below is a simple example that should explain what I want to do. Using the example below, I want to mock ReturnInput , while testing ReturnInputPlus1 . using ::testing::Invoke; using ::testing::_; using ::testing::Return; class MyClass { public:

How to mock variadic functions using googlemock

徘徊边缘 提交于 2019-12-21 01:57:12
问题 Not so much a question as a piece of knowledge sharing. According to the GoogleMock FAQ it is not possible to mock variadic functions since it is unknown how many arguments will be given to the function. This is true, but in most cases one knows with how much variables the variadic function is called from the system-under-test or how to transform the variadic arguments to 1 non-variadic argument. A colleague of mine (don't know if he is active on Stackoverflow) came up with a working solution

What is the difference between gtest and gmock?

北城以北 提交于 2019-12-20 10:21:54
问题 I'm trying to understand the purpose of google-mock , Google's C++ mocking framework. I have already worked with gtest earlier, but still I can't understand what gmock is. Why do we need it? gtest is used for unit testing. What do we need gmock for then, if gmock is required for unit testing ? 回答1: "Google Mock is not a testing framework itself. Instead, it needs a testing framework for writing tests. Google Mock works seamlessly with Google Test. It comes with a copy of Google Test bundled.

What is wrong with my attempts to mock a simple C++ method with googlemock?

╄→гoц情女王★ 提交于 2019-12-20 06:09:14
问题 As per Patterns for unit testing a C++ method that makes a standard library call, I'm test-driving development of a network-abstracting class. In order to unit test code that makes standard C library calls (which I can't mock) to handle BSD sockets, I've defined an interface ISocket from which both my real implementation CSocket and mock MockSocket inherit. Now I write my first unit test for the Network class, which uses an ISocket for the heavy lifting: #include "gmock/gmock.h" #include

GoogleMock: Expect either of two method calls

旧街凉风 提交于 2019-12-19 21:55:11
问题 I have a class Foo that references multiple other objects of type IBar . The class has a method fun that needs to invoke method frob on at least one of those IBar s. I want to write a test with mocked IBar s that verifies this requirement. I'm using GoogleMock. I currently have this: class IBar { public: virtual void frob() = 0; }; class MockBar : public IBar { public: MOCK_METHOD0(frob, void ()); }; class Foo { std::shared_ptr<IBar> bar1, bar2; public: Foo(std::shared_ptr<IBar> bar1, std:

Mock non-virtual method giving compilation error

回眸只為那壹抹淺笑 提交于 2019-12-19 10:09:20
问题 I need to write the gtest to test some existing code that has a non-virtual method, hence I am testing using the below source, but I am getting the compilation error package/web/webscr/sample_template_class3.cpp: In function âint main()â: package/web/webscr/sample_template_class3.cpp:64: error: âclass Templatemyclassâ has no member named âgmock_displayâ sample_template_class3.cpp #include <iostream> #include <gtest/gtest.h> #include <gmock/gmock.h> using namespace std; template < class

Uninteresting mock function call bla() && Expected: to be called at least once bla()?

冷暖自知 提交于 2019-12-18 08:31:30
问题 I've written a small test with a mocked class. When I run it, first I get the warning that an uninteresting mock function was called and then the test fails because the expectation is not met, which is that the mocked function is called at least once. The funny thing is that that function is called as I see that warning message above. Do you have any ideas on this matter? Thank you! Edit: This is my code structure: class Bla { public: Bla(); virtual ~Bla(); virtual float myFunction(); } class