0

我正在测试 GTEST,我在其中模拟调用函数和类,但不调用模拟函数,而是调用原始函数。

我创建了模拟类,我还更新了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);
}
4

1 回答 1

2

声明中使用的损坏名称

MoneyInstrument::UserMoneyInstrumentMapPBPtr  __wrap__ZN19MoneyInstrument31getUserMoneyInstrumentMapPBEP18TransactionHandlerP15BusinessContextRK7DBHost(TransactionHandler *_th, BusinessContext *_context, const DBHost &_db);

以及函数的定义,

Money::UserMoneyMapPBPtr  __wrap__ZN19Money31getUserMoneyMapPBEP18TransactionHandlerP15BusinessContextRK7DBHost(TransactionHandler *_th, BusinessContext *_context, const DBHost &_db)

不一样。

GTest 将调用原始函数,因为您在 extern "C" 块中声明了损坏的函数名称,但未在 Mocked 类中给出相同的定义。添加与 extern 块中具有相同名称的函数定义将解决此问题。

于 2013-09-23T07:51:51.423 回答