Skip to content

code-in-cpp/gmock-free

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gmock-free

gmock-free Build Status

This header-only library implements gmock functionality for global functions.

Introduction

This library is inspired by gmock-global with nicer API.

Gmock is a C++ framework for writing mock classes. It is very convenient to create mock objects for mocking of methods. But gmock can not mock global functions. This problem is quite common but has no trivial solution. Gmock FAQ says you are doing something wrong if you need to mock static or global functions. However it is required in some cases and gmock-free provides such functionality.

Usage

Step 1: Adding includes

At first your project needs to know about gmock-free.

  1. Add gmock-free/include to the project include paths.
  2. Add #include <gmock-free/gmock-free.h> after gmock include.

Step 2: Declare mock free

Syntax is most similar to gmock. For example, to mock function multiply with two double arguments and double result you have to write declaration:

MOCK_FREE_FUNC(double, multiply, (double, double));

You can check call count of such function using EXPECT_FREE_CALL macro, same as you used EXPECT_CALL macro for classes:

EXPECT_FREE_CALL(multiply, (1, 2));

The .Times(...) and other methods will be work too.

In result mocking of global double multiply(double, double) looks like:

MOCK_FREE_FUNC(multiply, double(double, double));

...

TEST(TestGlobal, CanMultiplyGlobal)
{
    EXPECT_FREE_CALL(multiply(1, 2)).Times(1);
    multiply(1, 2);
    GlobalMockObject::Verify();
}

Also you can use ON_FREE_CALL to specify default behavior.

gmock-free supports more than 15 arguments with gtest version 1.11.0.

Known issues

Samples

Samples live in the test code

License

gmock-free is licensed under the MIT License. You can freely use it in your commercial or opensource software.

Version history

Version 0.1.0 (25 Sep 2021)

  • Initial public release

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published