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
Related
In the following example, I would like to check if fooFunction is called upon destruction of the object.
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class FooClass
{
public:
~FooClass(){ FooClass::fooFunction(); }
virtual void fooFunction() {}
};
class MockFooClass : public FooClass
{
public:
MOCK_METHOD0(fooFunction, void());
};
TEST(DumbTest, blabla)
{
auto mock = new MockFooClass;
EXPECT_CALL(*mock, fooFunction());
delete mock;
}
However this test fails because the destructor of FooClass calls FooClass::fooFunction() instead of MockFooClass::fooFunction().
Does anyone know of a way to do that?
I'm using Visual Studio 2010.
I understand why this test fails: as soon as we enter the destructor of FooClass, the object is not a MockFooClass any more. What I'm looking for is if anyone knows of a way to get around it.
You can use Isolator++ to use check that the method was called.
class FooClass
{
public:
~FooClass(){ FooClass::fooFunction(); }
virtual void fooFunction() {}
};
TEST(DumbTest, blabla)
{
auto real = new FooClass;
auto mock = FAKE_ALL<FooClass>(FakeOptions::CallOriginal);
delete real;
ASSERT_WAS_CALLED(mock->fooFunction());
}
#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.
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);
}
I have see some sample code about gmock,
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <stdio.h>
#include <string>
class MockFoo {
public:
MockFoo() {}
MOCK_METHOD3(Bar, char(const std::string& s, int i, double x));
MOCK_METHOD2(Bar2, bool(int x, int y));
MOCK_METHOD2(Bar3, void(int x, int y));
private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
};
class GMockOutputTest : public testing::Test {
protected:
MockFoo foo_;
};
TEST_F(GMockOutputTest, ExpectedCall) {
testing::GMOCK_FLAG(verbose) = "info";
EXPECT_CALL(foo_, Bar2(0, _));
foo_.Bar2(0, 0); // Expected call
testing::GMOCK_FLAG(verbose) = "warning";
}
The mock class MockFoo is mocking three functions as is in the class body, but the class MockFoo does not inherit any class.
If I understand it correctly, the mock class can mock virtual and non-virtual functions.
1) Mocking virtual functions: mock class should inherit a base class being mocked, and all the functions in that class should be virtual, but in this example, there is no base class.
2) Mocking non-virtual functions: mock class does not need inherit any class, but it needs to add tamplate to the code in order to use hi-perf dependency injection.
Does the code above belong to any of the case? And what is it trying to mock? And how to understand it?
The key thing to remember here is that a mock class can override either virtual or non-virtual methods in a superclass, but it doesn't have to. Sometimes it's useful to define a totally standalone mock which doesn't conform to any particular interface or override any other functionality. Consider the case where you are testing a templatized class:
template <typename T>
class Foo {
public:
Foo(T* member) : _member(member) {}
void f() { _member->bar(); }
private:
T* _member;
};
You want to verify that class Foo invokes bar() on the template parameter class, but the template class doesn't need to conform to any formal interface; it just needs to expose a bar() method. In this case you might use a standalone mock:
class MockSomething {
public:
MOCK_METHOD0(bar, void());
};
Just like your example, this mock class has no relationship to another class or interface. It's a standalone mock, and it can be used just as you would any other:
TEST(StackOverflow, StandaloneMock) {
MockSomething mock;
EXPECT_CALL(mock, bar());
Foo<MockSomething> foo(&mock);
foo.f();
}
That code is to test the method Bar2() of template class NaggyMock. To be more precise, it is testing that when you call method Bar2() with parameters 0 and 0, that Bar2() method from the mock is going to be called.
You can look at that as a static dependency injection, since you inject dependency through a template parameter. I would say it is the case 2.
Assuming your template class NaggyMock looks like this :
template< typename Foo >
class NaggyMock
{
public:
bool Bar2(int x, int y)
{
if ( ( 0 == x ) && ( 0 == y ) )
{
return foo.Bar2( 0, 11 );
}
return false;
}
private:
Foo foo;
};
then in the unit test you are testing the case when both parameters are 0. If your real class is using some resource (reading from a network, or from a database, or a file, ...), then to avoid it, and make your unit test very fast, you are going to mock that object. And that is what you are doing. Instead of the class handing the real resource, you are injecting a mock object.
For more information, read :
What is Mocking?
Why prefer template method over dependency injection?
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.