Coder Social home page Coder Social logo

gmock-global's Introduction

gmock-global Build Status

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

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.

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.

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

Samples live in the separate repository

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

gmock-global's People

Contributors

grivus avatar sergiusthebest avatar

Watchers

 avatar  avatar

Forkers

vshylienkov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.