How can i return a derived class pointer as an output parameter?

365 views
Skip to first unread message

Mano Nathan

unread,
Oct 11, 2011, 8:50:54 PM10/11/11
to Google C++ Mocking Framework
Hi All,
I have a ClassB which derives from ClassA. When I call the mock
object, I want the mock to return a derived class pointer. I am using
SetArgPointee to set the out parameter. I can return base class
pointer but it fails with derived class pointer.
I am new to gmock. What is the best way to do this?
Thanks,
Mano

class ClassA
{
public:
ClassA(void);
virtual ~ClassA(void);
int iValueA;
};

class ClassB : public ClassA
{
public:
ClassB(void);
~ClassB(void);
int iValueB;
};

class IFoo
{
public:
virtual ~IFoo(){}
virtual void MethodTest(ClassA *pClassA) = 0;
};

class CMockFoo : public IFoo
{
public:
MOCK_METHOD1(MethodTest, void(ClassA* pMessage));
}

CMyTestClass::CMyTestClass(IFoo *pFoo)
{
m_pFoo = pFoo;
}

CMyTestClass::~CMyTestClass(void)
{
}

void CMyTestClass::MyTestMethod()
{
ClassB b;
b.iValueA = 888;
b.iValueB = 999;

// After calling this method I want the values set by the
SetArgPointee
m_pFoo->MethodTest(&b);

// Here i want to return the derived class pointer from the
test.
ATLASSERT(b.iValueB == 25);
ATLASSERT(b.iValueA == 10);
}

}


SUITE(MySuite)
{
struct MyFixture
{
IFoo* m_pFoo;
CMyTestClass *m_pMyTestClass;
MyFixture()
{
m_pFoo = new NiceMock<CMockFoo>();
m_pMyTestClass = new CMyTestClass(m_pFoo);
}
~MyFixture()
{
delete m_pFoo;
m_pFoo = NULL;

delete m_pMyTestClass;
m_pMyTestClass = NULL;
}
}

TEST_FIXTURE(MySuite, TestA)
{
ClassB msg;
msg.iValueA = 10;
msg.iValueB = 25;

EXPECT_CALL(*m_pFoo, MethodTest(_))
.WillOnce(SetArgPointee<0>(msg);

m_pMyTestClass->MyTestMethod();
}
}

Keith Ray

unread,
Oct 11, 2011, 9:06:48 PM10/11/11
to Mano Nathan, Google C++ Mocking Framework
To return a pointer to a subclass instance via a pointer argument, you need a pointer-to-a-pointer or reference-to-pointer

virtual void MethodTest(ClassA **pClassA) 

C. Keith Ray
 
Amplify Your Agility
Coaching | Training | Assessment | eLearning 

On Oct 11, 2011, at 5:50 PM, Mano Nathan <mano.n...@gmail.com> wrote:

virtual void MethodTest(ClassA *pClassA)

Mano Nathan

unread,
Oct 11, 2011, 10:18:34 PM10/11/11
to Google C++ Mocking Framework

Hi Kathy,
May be I didn’t explain it very well. What I really want is that when
I call the mock method it should inject my expectation and the pointer
should point to the new value set in the expectation.

In method “CMyTestClass::MyTestMethod”, After I call the m_pFoo-
>MethodTest(&b) it should change to the value set in the expectation.
Eg: Before calling m_pFoo->MethodTest(&b) method the values should be
b.iValueA = 888 and b.iValueB = 999. Once this method is executed the
values should change to b.iValueB == 25 and b.iValueA == 10.
Thanks,
Mano



On Oct 11, 6:06 pm, Keith Ray <keith....@gmail.com> wrote:
> To return a pointer to a subclass instance via a pointer argument, you need a pointer-to-a-pointer or reference-to-pointer
>
> virtual void MethodTest(ClassA **pClassA)
>
> C. Keith Ray
>
> Amplify Your Agility
> Coaching | Training | Assessment | eLearninghttp://industriallogic.com
Reply all
Reply to author
Forward
0 new messages