Create Mock for a constant method in Turtle - c++

I have,
class CFoo : public CFooPar
{
public:
CFoo(){}
~CFoo(){}
virtual bool ret() const
{
return true;
}
};
How can I create mock class for this virtual bool ret() const method?
Thank you!

I use Google Mock for that (https://code.google.com/p/googlemock/wiki/V1_6_ForDummies).
With that tool, the mock reads
#include "gmock/gmock.h"
class MockCFoo : public CFoo {
public:
MOCK_CONST_METHOD0(ret, bool());
};

If you mean using turtle here it is :
#include <turtle/mock.hpp>
MOCK_BASE_CLASS( MockCFoo, CFoo )
{
MOCK_METHOD( ret, 0 )
};
The rest depends on how you use CFoo in your production code, however it would likely be similar to the turtle motivation case I suppose.

Related

Mocking static function member

I'm trying to mock a static function member with gmock. I found a section on the gMock Cookbook that talks about Mocking Free Functions. However it is not clear to me..
For instance, I have a static function called isActiveMode that I want to mock. Instead of calling isActiveMode directly, I have to introduce an interface for it and have a concrete subclass that calls the static function:
class MyClassInterface {
public:
...
virtual bool isActive() = 0;
};
class MyClass: public MyClassInterface {
public:
...
virtual bool isActive()
{
return isActiveMode();
}
};
Then I define the mock class:
class MyMock {
public:
MOCK_METHOD(bool, isActive, (), (override));
};
But this does not seem to work..
Does somebody have a clue on how to make this work?
Your mock class should be derived from the interface class.
class MyMock : public MyClassInterface {
public:
MOCK_METHOD(bool, isActive, (), (override));
};
If that doesn't help, please provide a minimum reproducible example showing how you are using the mock.

gMock upcasting fails on EXPECT_CALL

I am running into a problem with UpCasting with Gmock
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using namespace testing;
using namespace std;
class MyClass {
public:
MyClass(int xxx) {}
int call(int x) {
};
};
class MockMyClass : public MyClass {
public:
MockMyClass(int xxx) : MyClass(xxx) {};
MOCK_METHOD1(call, int(int));
};
TEST(TestMyClass, worksFine) {
MockMyClass mock(111);
EXPECT_CALL(mock, call(_))
.WillOnce(Return(2000)); // times(1) by default;
mock.call(23);
}
TEST(TestMyClass, doesntWork) {
MockMyClass mock(111);
MyClass &myClassNotMock = mock;
EXPECT_CALL(mock, call(_))
.WillOnce(Return(2000)); // times(1) by default;
myClassNotMock.call(23);
}
I have tried to use pointers and it looks like it is the same problem. Everytime I have Base object reference (upcasting from mock) and I call a function on it gmock will not pick it up.
I would appreciate some help
Ok so I have managed to find out what was the problem. Because MyClass call function isn't virtual I have to use templating technique explained in Google Mock CookBook mocking non virtual methods or simply introduce an interface for MyClass and mock it normally

googlemock: mock a local object

#include "gtest/gtest.h"
#include "gmock/gmock.h"
class Turtle{
public:
int foo();
};
int func(){
Turtle local_tutrtle;
auto x = local_tutle.foo();
......
return x;
}
TEST(mock, foo) {
class MockTurtle : public Turtle {
public:
MOCK_METHOD0(foo, int());
};
ASSERT_EQ(10, func());
}
How can I mock the local_turtle in func()? I want to change the return value of local_tutle.foo() without modifying func();
Thanks.
You can't.
You have to supplement mocked object (in your example turtle) from the outside, by passing (mocked or nomral) turtle object as an argument.
This design pattern is called dependency injection, and you should get familiar with it as soon as possible, because it's very important in designing easily testable applications and frequently used.

How to test classes that is derived from external base class which depends on external system

I have a class that is subclass of an external class over which I don't have any control. The external class depend on system resources. For example
class MyClass : public ExternalBase // This class is from external framework and framework requires it to derive from this class.
{
int doSomePrivateThing(int );
public:
virtual int DoSomething(int );
virtual ~MyClass();
}
int MyClass::doSomePrivateThing(int )
{
// do some private task
}
int MyClass::DoSomething(int n)
{
// Do MyClass Specific task
int k = doSomePrivateThing(n);
return ExternalBase::DoSomething(k); // This function depends on external system resources.
// Probably try to communicate with remote server
// or attempt access Storage or Display device etc.
}
MyClass::~MyClass()
{}
How can I break the dependency of MyClass and write unit test for MyClass::DoSomething(). Using composition in place of inheritance is not a choice as framework requires classes to be derived from this base class.
I am using C++ and GoogleTest/Mock. But any generalized solution is appreciated.
Thanks in advance.
There are two ways. I call them "a little more correct" way and "very ugly" way.
The "more correct" way:
Enclose external class functions with some additional layer than can be partial mocked.
class MyClass : public ExternalBase // This class is from external framework and framework requires it to derive from this class.
{
int doSomePrivateThing(int );
public:
virtual void BaseDoSomething(int) { return ExternalBase::DoSomething(v); }
virtual int DoSomething(int v);
virtual ~MyClass();
};
int MyClass::DoSomething(int n)
{
// Do MyClass Specific task
int k = doSomePrivateThing(n);
return BaseDoSomething(k);
}
And partial mock in UT in this way:
class TestableMyClass : public MyClass
{
public:
using MyClass::MyClass;
MOCK_METHOD1(BaseDoSomething, int(int));
};
TEST(A,A)
{
TestableMyClass objectUnderTest;
EXPECT_CALL(objectUnderTest, BaseDoSomething(112));
objectUnderTest.DoSomething(112);
}
When you need to call also the true base class method in your test - use WillOnce(Invoke...) with EXPECT_CALL.
The "very ugly" way:
Provide your own UnitTest implementation of ExternalBase and link it to your test. This "UnitTest" impolementation of ExternalBase should be based on some global Mocks objects.
ExternalBaseMock.hpp:
class ExternalBaseMock
{
public:
MOCK_METHOD1(DoSomething, int(int));
};
extern ExternalBaseMock externalBaseMock;
ExternalBaseMock.cpp:
ExternalBaseMock externalBaseMock;
int ExternalBase::DoSomething(int n)
{
return externalBaseMock.DoSomething(n);
}
Then your tests:
#include "ExternalBaseMock.hpp"
TEST(A,A)
{
MyClass objectUnderTest;
EXPECT_CALL(externalBaseMock, DoSomething(112));
objectUnderTest.DoSomething(112);
}

How to test method in google test, using std::function?

I would like to test method "methodToTest" in class A:
typedef std::function F_global;
struct A
{
F_global m_F_global;
A(F_global m_F_global) : p_F_global(m_F_global) {}
void methodToTest()
{
m_F_global(5);
}
};
I have got a mock class:
class RunMock
{
public:
MOCK_METHOD1(run, void (int));
};
Below I have got a test case:
class TestClass : public testing::Test
{
protected:
void SetUp();
std::unique_ptr<A> m_aa;
std::unique_ptr<RunMock> m_runMock;
};
void UecSimplePortTestSuite::SetUp()
{
m_aa = make_unique<A>(m_runMock->run);//IT DOESN'T WORK I DON'T KNOW HOW TO FORWARD A METHOD run from RunMock to constructor
}
TEST_F(UecSimplePortTestSuite, testForwardMessage)
{
EXPECT_CALL(*m_runMock, run(_));
m_aa->methodToTest();
}
Generally I don't know how transfer a method "run" from Mock class "RunMock" in "UecSimplePortTestSuite::SetUp()". I would like to do this, because
I would like to "EXPECT_CALL(*m_runMock, run(_));" in UecSimplePortTestSuite.testForwardMessage to test "methodToTest()".
I think that good idea could be to use lmbda, std::boost::bind or something like this.
You can pass a lambda to the constructor of A in the SetUp() :
m_runMock.reset( new RunMock );
m_aa.reset( new A( [&](int value){ m_runMock->run(value); } );
This :
m_runMock->run
is not going to do what you thing, and it is a compilation error. You can read this parashift QA and here how to use pointer to member function.