#include <iostream>
using namespace std;
#include <string>
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using ::testing::AtLeast;
using ::testing::Return;
using ::testing::_;
class User{
public:
string Name;
virtual void SetName(string tmpName){Name=tmpName;}
virtual string GetName(){return Name;}
virtual bool IsAdmin(){
string admin="alley";
if(admin.compare(Name)==0)//the contents of both strings are equal
return true;
return false;
}
};
class MockUser:public User
{
public:
MOCK_METHOD1(SetName, void(string tmpName));
MOCK_METHOD0(GetName, string());
MOCK_METHOD0(IsAdmin, bool());
};
class Reservation{
public:
User &BookName;
Reservation(User user):BookName(user){}
bool CanBeCancelBy(User cuser){
cuser.IsAdmin();
if(cuser.IsAdmin()==true)
return true;
if(cuser.GetName().compare(BookName.GetName())==0)//the contents of both strings are equal
return true;
return false;
}
};
TEST(CanBeCancelBy, MockUserIsAdmin_ReturnTrue)
{
//Arrange
MockUser admin;
User a1;
a1.SetName("a1");
Reservation r1(a1);
EXPECT_CALL(admin, IsAdmin()).Times(1).WillOnce(Return(true));
//Act
//bool result =admin.IsAdmin();
bool result=r1.CanBeCancelBy(admin);
//Assert
EXPECT_EQ(result, 1);
}
int main()
{
testing::InitGoogleTest();
return RUN_ALL_TESTS();
}
I think IsAdmin() will be called, but it doesn't.
The execute result as bellow:
[ RUN ] [mCanBeCancelBy.MockUserIsAdmin_ReturnTrue Reservation.cpp:56: Failure Expected equality of these values: result Which is: false 1 Reservation.cpp:51: Failure Actual function call count doesn't match EXPECT_CALL(admin, IsAdmin())... Expected: to be called once Actual: never called - unsatisfied and active [ FAILED ] [mCanBeCancelBy.MockUserIsAdmin_ReturnTrue (3 ms)
Can someone help me to understand what's going wrong?