我想要測試一個基類的方法是否使用Google Mock測試調用並執行。我有一個簡單的BankAccount類來實現一個函數撤銷。在BankAccount.h文件:如何測試一個基類的方法是否已被調用並使用谷歌模擬執行?
class BankAccount {
public:
BankAccount();
int withdraw(int balance, int withdrawalAmount);
};
在BankAccount.cpp文件:
#include "BankAccount.h"
BankAccount::BankAccount()
{
}
int BankAccount::withdraw(int balance, int withdrawalAmount)
{
if (withdrawalAmount <= balance)
{
balance -= withdrawalAmount;
}
return balance;
}
在test.h文件我有:
#include "BankAccount.h"
class MockBankAccount : public BankAccount {
public:
MockBankAccount();
MOCK_METHOD2(withdraw, int(int balance, int withdrawalAmount));
};
我MockBankAccount類從繼承的BankAccount類。
在我TEST.CPP文件:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "test.h"
using namespace testing;
// Constructors/Destructors
MockBankAccount::MockBankAccount()
{
}
TEST(WithdrawAccountTest, Withdraw)
{
MockBankAccount mockAccount;
EXPECT_CALL(mockAccount, withdraw(5, 1))
.Times(1);
mockAccount.withdraw(5, 1);
}
// Main
int main(int argc, char* argv[])
{
InitGoogleTest(&argc, argv);
InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
我要檢查撤回方法被調用,並從BankAccount類(即的BankAccount ::撤回執行)執行。當我運行測試時,它會通過,我期望撤回已被調用並執行,但是,如果我在BankAccount :: withdraw和debug上放置斷點,我可以看到它從未實際從基類到達該方法。有沒有辦法使用Google Mock來檢查BankAccount :: withdraw,例如使用其他方法(組合而不是繼承,模板等)?