0
#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?

Quarra
  • 2,527
  • 1
  • 19
  • 27
alley
  • 11
  • 3
  • If I modify like this, bool result =admin.IsAdmin(); //bool result=r1.CanBeCancelBy(admin); unit test will be success. – alley Mar 04 '20 at 13:17
  • 1
    Polymorphism in C++ only works when you are using *pointers* or *references*. `CanBeCancelBy` *always* takes an object of type `User` and nothing else. When you pass a derived class there, it gets sliced to a regular class of `User` type. Looking for a good duplicate now. – Yksisarvinen Mar 04 '20 at 13:24
  • 2
    [Why doesn't polymorphism work without pointers/references?](https://stackoverflow.com/questions/15188894/why-doesnt-polymorphism-work-without-pointers-references) [What is object slicing?](https://stackoverflow.com/questions/274626/what-is-object-slicing) – Yksisarvinen Mar 04 '20 at 13:27
  • Also, `BookName` is a dangling reference as soon as constructor finishes. You cannot ever use it outside of constructor, because the object it refers to is dead. – Yksisarvinen Mar 04 '20 at 13:30
  • Test success when I modify like that bool CanBeCancelBy(User &user) thanks. – alley Mar 04 '20 at 15:42

0 Answers0