googlemock

How to mock method with optional parameter in Google Mock?

半城伤御伤魂 提交于 2019-12-04 16:29:40
问题 How to mock a method with optional parameter in Google Mock ? For example: class A { public: void set_enable( bool enabled = true ); }; class MockA : public A { MOCK_METHOD1( set_enable, void( bool ) ); // this is not working }; 回答1: This is an alternative of Marko's answer: If you don't want to change your original code, just implement the helper in the mock class: class A { public: virtual void set_enable( bool enabled = true ); }; class MockA : public A { MOCK_METHOD1( set_enable_impl,

C++ High performance unit testing with Google Mock?

烈酒焚心 提交于 2019-12-04 08:47:06
I'm using Google Mock, and I'm struggling to mock out C++ system calls (specifically the C++11 chrono functions). I'm know I'm supposed to make an interface, create a class to implement the interface for my actual implementation, and then mock out the interface on my tests. I'm trying to write an embedded application, so this level of indirection sounds too expensive for me. What is the most efficient/performant way to incorporate system calls into Google Mock? No, you don't need to resort to mocked static classes - that's one of many options. If you're in an embedded environment where a

Mocking an entire library

折月煮酒 提交于 2019-12-04 07:51:45
I'm developing code that uses boost::asio . To test it, I need to mock a set of classes from this library. I'm using Google Mock, which allows for mocking virtual methods. The usual (and tedious) process would be to write an interface for each of the classes I need to use. On the other hand, the Google Mock Cookbook describes an alternative when it comes to mocking non-virtual methods: using templates. The problem in my case is that I might need to mock several classes at the same time (so using templates directly wouldn't work). So I thought: why not use two-levels of templates? I came up

GoogleMock and GoogleTest in Visual Studio 2010

偶尔善良 提交于 2019-12-03 21:46:02
Has anyone successfully built gmock and gtest in Visual Studio 2010? I've tried with version 1.5.0, but I only get incomprehensible compilation errors. I found this thread in google groups about issues found when building gmock-1.5.0 under VS2010. Following the thread, I've created a short readme file, which worked for me, so here it is: Download gmock 1.5.0 from Google Mock . Extract to library folder on the machine (e.g. C:\Libs\gmock-1.5.0). From now on, this folder will be reffered as 'GMOCK_ROOT'. Open VS2010, and load the solution: GMOCK_ROOT\msvc\gmock.sln. Let VS convert it from VS2008

How to mock variadic functions using googlemock

一个人想着一个人 提交于 2019-12-03 07:04:17
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 as depicted in the example below (using a mock for an C-type interface): class MockInterface { public:

What is the difference between gtest and gmock?

淺唱寂寞╮ 提交于 2019-12-02 23:26:46
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 ? "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. Starting with version 1.1.0, you can also use it with any C++ testing framework of your choice. " - Google

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

流过昼夜 提交于 2019-12-02 10:29:33
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 "gtest/gtest.h" // C standard library includes omitted #include "MockSocket.h" #include "Network.h" using

Mock non-virtual method giving compilation error

…衆ロ難τιáo~ 提交于 2019-12-01 09:41:09
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 myclass> class Templatemyclass { private: myclass T; public : void display() { T.display(); } }; class Test {

Mocking non-virtual methods in C++ without editing production code?

て烟熏妆下的殇ゞ 提交于 2019-11-30 08:00:54
问题 I am a fairly new software developer currently working adding unit tests to an existing C++ project that started years ago. Due to a non-technical reason, I'm not allowed to modify any existing code. The base class of all my modules has a bunch of methods for Setting/Getting data and communicating with other modules. Since I just want to unit testing each individual module, I want to be able to use canned values for all my inter-module communication methods. I.e. for a method Ping() which

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

僤鯓⒐⒋嵵緔 提交于 2019-11-29 14:31:00
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 MockBla : public Bla { MockBla(); ~MockBla(); MOCKMETHOD0(myFunction, float()); } class CallerClass {