Reputation: 59
I am using cmake v3.19.2, gtest v1.10.0. I encountered this issue when i was building a class(let it be someClass) and tried mocking another class(otherClass) whose pointer object was in the same class(someClass) and tried seeing the number of times it calls the method. As i can see that method call isn't counted. I managed to isolate the issue:
someClass.h:
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include "otherClass.h"
//has an object from the other class
class someClass{
public:
someClass( otherClass* oc )
: toCheck{oc}
{}
float method_to_forward( int id ) const;
private:
otherClass* toCheck;
};
#endif
someClass.cpp
#include "someClass.h"
float someClass::method_to_forward( int id ) const{
return toCheck->method_to_check(id);
}
otherClass.h
#ifndef OTHERCLASS_H
#define OTHERCLASS_H
class otherClass{
public:
otherClass( float val )
:val{val}
{}
virtual float method_to_check( int id ) const;
private:
float val;
int id;
};
#endif
otherClass.cpp
#include "otherClass.h"
float otherClass::method_to_check( int id ) const{
return this->val;
}
Main.cpp
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "otherClass.h"
#include "someClass.h"
class mockOtherClass: public otherClass{
public:
mockOtherClass( float val )
:otherClass(val)
{}
MOCK_METHOD( float, method_to_check, (int) );
};
TEST( test_1, testOther ){
float val{20.00};
mockOtherClass mo{val};
someClass sc(&mo);
EXPECT_CALL(mo, method_to_check(testing::_)).Times(1);//.WillOnce(testing::Return(val));
float retVal = sc.method_to_forward( 2 );
ASSERT_EQ(retVal, val);
}
int main(int argc,char* argv[]){
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.19.2)
project( justTest VERSION 1.0 )
find_package(GTest REQUIRED)
include_directories( ${GTEST_INCLUDE_DIRS})
find_package(GMock REQUIRED)
include_directories( ${GMOCK_INCLUDE_DIRS})
add_executable(testRunner
Main.cpp
otherClass.cpp
someClass.cpp
)
target_link_libraries(testRunner
${GTEST_LIBRARIES}
${GMOCK_BOTH_LIBRARIES}
pthread)
the faliure that i get
Actual function call count doesn't match EXPECT_CALL(mo, method_to_check(testing::_))... Expected: to be called once Actual: never called - unsatisfied and active
i am really sorry you had to go through all the code. Thanks in advance.
Upvotes: 0
Views: 405
Reputation: 59
I am really really sorry for wasting you guy's time the only issue was that there is a variant of MOCK_METHOD which is MOCK_CONST_METHOD(no of args) which works just fine. is there any other way of doing like if we want to write a mock of non const method we write MOCK_METHOD instead of MOCK_METHOD1 just the args change so is there any other such variant for the above method which works in the same way
Upvotes: 1