我正在测试GTEST,我正在模拟调用函数和类,但是没有调用mock函数,而是调用原始函数。
我创建了模拟类,我也更新了linkopt 如下..
linkopt -W1, - wrap = _ZN19MoneyInstrument31getUserMoneyInstrumentMapPBEP18TransactionHandlerP15BusinessContextRK7DBHost
namespace MoneyInstrument
{
class MockUserMoneyInstrumentMapPB : public UserMoneyInstrumentMapPB
{
public:
static UserMoneyInstrumentMapPBPtr m_user_Money_instrument_map_pb;
MockUserMoneyInstrumentMapPB(TransactionEntity *_th, ProductContext * _context):UserMoneyInstrumentMapPB(_th, _context)
{
}
static void set_instance(MockUserMoneyInstrumentMapPB* _user_Money_instrument_map_pb)
{
std::cout<<__FILE__ << " " << __FUNCTION__ << " " << __LINE__ << std::endl;
}
MOCK_CONST_METHOD1(load_by_account_number,TArray<UserMoneyInstrumentMapBDOPtr>(const ullong _account_number));
}
}
MoneyInstrument::UserMoneyInstrumentMapPBPtr MoneyInstrument::MockUserMoneyInstrumentMapPB::m_user_Money_instrument_map_pb;
extern "C"{
MoneyInstrument::UserMoneyInstrumentMapPBPtr __wrap__ZN19MoneyInstrument31getUserMoneyInstrumentMapPBEP18TransactionHandlerP15BusinessContextRK7DBHost(TransactionHandler *_th, BusinessContext *_context, const DBHost &_db);
Money::UserMoneyMapPBPtr __wrap__ZN19Money31getUserMoneyMapPBEP18TransactionHandlerP15BusinessContextRK7DBHost(TransactionHandler *_th, BusinessContext *_context, const DBHost &_db)
{
return Money::MockUserMoneyMapPB::m_user_Money_instrument_map_pb;
}
load_preferences ( ...)
{
UserMoneyInstrumentMapPBPtr ufim_pb_ptr =
getUserMoneyInstrumentMapPB(m_th, m_context);
TArray<UserMoneyInstrumentMapBDOPtr> ufim_bdo_array;
try
{
ufim_bdo_array = ufim_pb_ptr->load_by_account_number(_account_number);
}
catch(const MoneyInstrumentException & fie)
{
throw MoneyInstrumentException(Exception,
"Not able to load data from WUSER_Money_INSTRUMENT_MAP Table");
}
}
TEST_F(bli_test,test1)
{
MockUserMoneyInstrumentMapPB *m_mock_ufim_pb = new MockUserMoneyInstrumentMapPB(m_th, m_context);
MockUserMoneyInstrumentMapPB::set_instance(m_mock_ufim_pb);
TArray<UserMoneyInstrumentMapBDOPtr> ufim_bdo_array;
EXPECT_CALL(*m_mock_ufim_pb,load_by_account_number(_)).WillOnce(Return(ufim_bdo_array));
m_mock_bli_test->load_preferences(account_number,preference_type,search,list);
}
答案 0 :(得分:2)
声明中使用的错位名称
MoneyInstrument::UserMoneyInstrumentMapPBPtr __wrap__ZN19MoneyInstrument31getUserMoneyInstrumentMapPBEP18TransactionHandlerP15BusinessContextRK7DBHost(TransactionHandler *_th, BusinessContext *_context, const DBHost &_db);
和函数的定义,
Money::UserMoneyMapPBPtr __wrap__ZN19Money31getUserMoneyMapPBEP18TransactionHandlerP15BusinessContextRK7DBHost(TransactionHandler *_th, BusinessContext *_context, const DBHost &_db)
不一样。
GTest将调用原始函数,因为您在外部“C”块中声明了受损函数名称,但未在Mocked类中给出相同的定义。添加具有与extern块中相同的受损名称的函数定义将解决此问题。