функция фиктивного класса не вызывается во время GTEST

Я тестирую 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);
}

person Gopal    schedule 09.05.2013    source источник


Ответы (1)


Искаженное имя, используемое в объявлении

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. Добавление определения функции с тем же искаженным именем, что и во внешнем блоке, решит эту проблему.

person Malathi    schedule 23.09.2013