GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - apriorit/gmock-global: Provides ability to mock global functions with gmock

BranchesTags
Open more actions menu

Latest commit

 

History

23 Commits
23 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
include/gmock-global
include/gmock-global
 
 
sample
sample
 
 
.gitignore
.gitignore
 
 
BUILD
BUILD
 
 
CMakeLists.txt
CMakeLists.txt
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
WORKSPACE
WORKSPACE
 
 

Repository files navigation

gmock-global Build Status

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

  • Introduction
  • Usage
    • Step 1: Adding includes
    • Step 2: Declare mock global
  • Known issues
  • Samples
  • License
  • Version history
    • Version 1.0.2 (28 Aug 2019)
    • Version 1.0.1 (19 Apr 2019)
    • Version 1.0.0 (17 Oct 2017)

Introduction

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-global provides such functionality.

Supported environment

  • gmock version 1.8.1-1.15.2 (older versions may work too but they are not covered by CI)
  • gcc/clang/msvc

Usage

Step 1: Adding includes

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

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

Step 2: Declare mock global

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_GLOBAL_FUNC2(multiply, double(double, double));

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

EXPECT_GLOBAL_CALL(multiply, multiply(1, 2));

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

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

MOCK_GLOBAL_FUNC2(multiply, double(double, double));

...

TEST(TestGlobal, CanMultiplyGlobal)
{
    EXPECT_GLOBAL_CALL(multiply, multiply(1, 2)).Times(1);
    multiply(1, 2);
}

Also you can use ON_GLOBAL_CALL to specify default behavior. ON_GLOBAL_NICE_CALL can be used to set default behavior with suppressed warning when this mock was actually called.

gmock-global supports more than 10 arguments with gtest version 1.8.1. But it requires gmock-more-args version 1.0.1 in case you use gtest version 1.8.0

Known issues

The after clause can't be using with gmock-global, use Sequences instead.

Samples

Take a look at the test sample.

License

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

Version history

Version 1.0.2 (28 Aug 2019)

  • Added ON_GLOBAL_CALL (#4)
  • Segmentation fault when executing tests on a function that has been mocked in another test (#2)

Version 1.0.1 (19 Apr 2019)

  • Specified compatibility with gtest 1.8.1
  • Fixed warnings

Version 1.0.0 (17 Oct 2017)

  • Initial public release

About

Provides ability to mock global functions with gmock

Topics

cppgmockgtestheader-onlylibrarymockmockingunittest

Resources

Readme
MIT license
Activity
Custom properties

Stars

75 stars

Watchers

13 watching

Forks

26 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.