无法使用 google 模拟 C++ 模拟 CDatabase Open/OpenEx

cannot mock CDatabase Open/OpenEx using google mock c++

我正在尝试模拟 Cdatabase 方法 "Open"。下面是我模拟 class 配置的文件。

afxdb.h

...
    virtual BOOL Open(LPCTSTR lpszDSN, BOOL bExclusive = FALSE,
            BOOL bReadonly = FALSE, LPCTSTR lpszConnect = _T("ODBC;"),
            BOOL bUseCursorLib = TRUE);
...

mock_myCDatabase.h

...<linkers>

class mock_my_CDatabase :public CDatabase
{
public:
    mock_my_CDatabase()
    {}
    ~mock_my_CDatabase()
    {}
    MOCK_METHOD5(Open, BOOL(LPCTSTR lpszDSN, BOOL bExclusive,BOOL bReadonly, LPCTSTR lpszConnect,BOOL bUseCursorLib));
};

DemoClassB.cpp

    ...<linkers>
  ...
    CDemoClassB::CDemoClassB(CDatabase  &p_db)
    {
        m_db = &p_db;
    }
  ...
  ...
    BOOL CDemoClassB::fun2()
    {
        m_db->Open((LPCTSTR)NULL, FALSE, FALSE, (LPCTSTR)"ODBC;DRIVER={MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)};DSN='';DBQ=C:\Karthik\GMockDemo\GMockDemo\RepolyticsDB.accdb", CDatabase::noOdbcDialog);
            return TRUE;
    }

GMockDemo.cpp

// GMockDemo.cpp : Defines the entry point for the console application.
//

...<linkers>

using namespace testing;
using ::testing::Return;


TEST(mock, mockCDatabase)
{
    mock_my_CDatabase mockDb;

    EXPECT_CALL(mockDb, Open((LPCTSTR)"ODBC;DRIVER={MICROSOFT ACCESS DRIVER (*.mdb, *.accdb)};DSN='';DBQ=C:\Karthik\GMockDemo\GMockDemo\RepolyticsDB.accdb", CDatabase::noOdbcDialog)).Times(8).WillRepeatedly(Return(TRUE));
    CDemoClassB m_Bt(mockDb);
    ASSERT_EQ(m_Bt.fun2(), TRUE);
}
...
...
int _tmain(int argc, _TCHAR* argv[])
{
    ::testing::InitGoogleMock(&argc, argv);
    RUN_ALL_TESTS();
    std::getchar(); // keep console window open until Return keystroke
    return 0;
}

我无法模拟 CDatabase 方法:Open。当我 运行 代码时,我得到

预期:调用一次 实际:从未致电 - 不满意且活跃

谁能帮我模拟 CDatabase 方法

您应该会在 mock_my_CDatabase::Open 收到 uninteresting function calls 的警告消息。检查测试输出。

尝试使用 string matcher 作为模拟函数参数。您正在尝试将第一个参数与指向文字的指针进行匹配,而后者永远不会匹配。