How to use gmock to mock up a std::function?

zeejan

The constructor of my class is

A( ...
   std::function<bool(const std::string&, const std::string&)> aCallBack, 
   ... );

I want to use EXPECT_CALL to test it. This callback is from another class B. I created a Mock like

class BMock : public B
{
    MOCK_METHOD2( aCallBack, bool(const std::string&, const std::string&) );
}

Then I tried

B *b = new B();
std::function<bool(const std::string&, const std::string&)> func = 
    std::bind(&B::aCallBack, b, std::PlaceHolders::_1, std::PlaceHolders::_2);

It still does not work. How can I get a function pointer of a gmock object?

Thanks!!

Enzam Hossain

If you want to use mock to keep track of calls and set return values, you can use MockFunction.

using testing::_;
using testing::MockFunction;
using testing::Return;

MockFunction<bool(const std::string&, const std::string&)> mockCallback;

EXPECT_CALL(mockCallback, Call(_, _)).WillOnce(Return(false)); // Or anything else you want to do

A( ...
   mockCallback.AsStdFunction()
   ...);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

gmock gtest how to setup the mock

From Dev

How to use gmock with xcode?

From Dev

GMock - how to refresh mock return values?

From Dev

How to mock calling to base class with gmock

From Dev

GMock, invoking std::function captured by SaveArg

From Dev

GMock: How to return mock class variable as the return value

From Dev

How to use Moq to mock up the StackExchange.Redis ConnectionMultiplexer class?

From Dev

How to mock up dbcontext?

From Dev

How to mock up dbcontext?

From Dev

How do I use GMock with dependency injection?

From Dev

How to use std::bind with std::function and std::map

From Dev

Mock in the Middle with C++/Gmock

From Dev

Mock in the Middle with C++/Gmock

From Dev

How pass std::endl to a function and use it?

From Dev

How to use a std::function as a C style callback

From Dev

How to use SWIG to wrap std::function objects?

From Dev

How to use template function parameter in std::bind?

From Dev

How can I use polymorphism with std::function?

From Dev

How to use template function parameter in std::bind?

From Dev

GMOCK - mock an object and its inside mock method

From Dev

How to mock a decorated function

From Dev

use of std::bind and std::function

From Dev

How to use gmock to test that a class calls it's base class' methods

From Dev

how to mock macro function using google mock

From Dev

how to mock macro function using google mock

From Dev

gmock: Returns distinct values on each mock invocation

From Dev

suppress gmock warning for a vector of mock class

From Dev

How do you mock up Throwable object?

From Dev

How to use this.up() function in button handler in Sencha Touch

Related Related

HotTag

Archive