mardi 3 mars 2015

How to write GMOCK google test cases for the below scenario

i have set of functions within the Singleton class. i want to mock a function in the singleton class. Lets take the below piece of code.The function setname() will return the string from the classyyy's setname() funciton. so here i want to test the return value.so please tell me how to write the test case for this situation.



class BTMxxx
{
private:
BTMxxx();
public:
string setname(const int& id, const string& name){
return classBTMyyyObject->setname(id, name);
}

};

class mockBtMxxx : public BTMxxx
{
public:
MOCK_METHOD2(setname, string(const int& id, const string& name));
};
// Test case for Setting Local Device Friendly Name.
TEST(TestBTC, GMockSetNameTest)
{
mockBtMxxx mock_Btm;
int id = 12345;
string str = "Hello";
EXPECT_CALL(mock_Btm, setname(_,_)).WillOnce(Return("Hello"));
}


I am getting the below errors : error: ‘BTMxxx::BTMxxx()’ is private gmock-actions.h:491:66: error: no matching function for call to ‘ImplicitCast_(const char*&)’


I am doing the gmock test in the eclipse environment. i want to mock the BTMxxx::setname() function and need to compare the return value with my expected value. Please see the overall file flow below


//file : BTMxxx.h



class RMxxx;
class BTMxxx
{
public:
/*! Destructor
*
*/
~BTMxxx( );
static BTMxxx* GetInstance();
string setname(const int& id, const string& name);
private:
/*! Constructor
*
*/
explicit BTMxxx( );
static BTMxxx* mSingleInstance;
RMxxx* m_RMxxx;

};


//file BTMxxx.cpp



string BTMxxx::setname(const int& id, const string& name)
{
return m_RMxxx->setname(id, name);
}


//file : RMxxx.h



class RMxxx
{
public:
/*! Constructor
* \brief Performs Initialization.
*/
explicit RMxxx( );
/*! Destructor
* \brief Frees the allocated memory
*/
~RMxxx();

string RMxxx::setname(const int& id, const string& name);
};


//file : RMxxx.cpp



string RMxxx::setname(const int& id, const string& name)
{
return name;
}


Here i should not modify the BTMxxx as protected. It strictly to be singleton class. Please tell me how to mock this setname() function inside the BTMxxx class and how to compare the return value with my expected value by using gmock.


Thank You in Advance. Please help me.


Aucun commentaire:

Enregistrer un commentaire