Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock - using ::testing::An()

I receive a compilation error while using the following Google Mock declaration:

EXPECT_CALL(some_object, someFunction(1,An<AStructIDefined>()))
    .Times(2);

The error is:

1>ClCompile:
1>  TestMyClass.cpp
1>TestMyClass.cpp(189): error C2664: 'mynamespace::MockMyClassClient::gmock_someFunction' : cannot convert parameter 2 from 'testing::Matcher<T>' to 'const testing::Matcher<T> &'
1>          with
1>          [
1>              T=mynamespace::AStructIDefined
1>          ]
1>          and
1>          [
1>              T=const mynamespace::AStructIDefined &
1>          ]
1>          Reason: cannot convert from 'testing::Matcher<T>' to 'const testing::Matcher<T>'
1>          with
1>          [
1>              T=mynamespace::AStructIDefined
1>          ]
1>          and
1>          [
1>              T=const mynamespace::AStructIDefined &
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

What am I doing wrong?


UPDATES:

I am using VS2010.

The declaration of someFunction is:

virtual void someFunction( long long ll, const AStructIDefined& a_struct);

An() is a Google Mock wildcard matcher with the following definition:

// Creates a matcher that matches any value of the given type T.
template <typename T>
inline Matcher<T> An() { return A<T>(); }

A simplified yet representative version of the struct is:

namespace mynamespace {

class ABaseCLass
{
public:
    virtual ~ABaseCLass(){};
    virtual bool isValid() const = 0;
};

struct AStructIDefined : public ABaseCLass
{
public:
    OrderStatusReport(SomeEnum1 e_, int i_, double d_);

    SomeEnum1 e;
    int i;
    double d;

    const std::string toString() const;
    bool isSane() const;
    bool operator== (const SomeEnum1& ref_) const;
    double getD() const;
    int getI() const;
    bool isCondition() const;
};

} // namespace mynamespace
like image 258
Jonathan Livni Avatar asked Feb 20 '11 10:02

Jonathan Livni


People also ask

How do you mock a function in C++?

It's possible to use Google Mock to mock a free function (i.e. a C-style function or a static method). You just need to rewrite your code to use an interface (abstract class). public: ... virtual bool Open(const char* path, const char* mode) { return OpenFile(path, mode); } };

How do you make a mock gMock?

Using the Turtle interface as example, here are the simple steps you need to follow: Derive a class MockTurtle from Turtle . Take a virtual function of Turtle (while it's possible to mock non-virtual methods using templates, it's much more involved). In the public: section of the child class, write MOCK_METHOD();

What is gMock and Gtest?

In real system, these counterparts belong to the system itself. In the unit tests they are replaced with mocks. Gtest is a framework for unit testing. Gmock is a framework imitating the rest of your system during unit tests.

What is Mock_method?

You must always put a mock method definition ( MOCK_METHOD ) in a public: section of the mock class, regardless of the method being mocked being public , protected , or private in the base class.


1 Answers

The solution was to change the declaration from:

EXPECT_CALL(some_object, someFunction(1,An<AStructIDefined>()))
    .Times(2);

to

EXPECT_CALL(some_object, someFunction(1,An<const AStructIDefined &>()))
    .Times(2);

C++ implicitly casts const and reference & on function parameters, but google mock's declaration seems to require the type appearing in the function's signature and not the type submitted as a parameter to the function.

like image 117
Jonathan Livni Avatar answered Oct 01 '22 03:10

Jonathan Livni