????MOCK_METHOD#1(#2?? #3(#4) )
????#2?????mock??????????#1??????mock????????м?????????#4???????????????????#3???????????????????????????????
// Account.h
// basic application data class
#pragma once
#include <string>
class Account
{
private:
std::string accountId;  //???
long balance;           //??? ?? ?????????
public:
Account();
Account(const std::string& accountId?? long initialBalance);
//????
void debit(long amount);
//????
void credit(long amount);
//??????
long getBalance() const;
//??????
std::string getAccountId() const;
};
???????????????????????????????????????????Account??debit?????????credit????????getBalance??????????getAccountId??????????????????
// A facility class acts as an external DB
class AccountHelper
{
private:
std::map<std::string?? Account> mAccount;
// an internal map to store all Accounts for test
public:
AccountHelper(std::map<std::string?? Account>& mAccount);
void updateAccount(const Account& account);
Account findAccountForUser(const std::string& userId);
};
AccountHelper::AccountHelper(std::map<std::string?? Account>& mAccount)
{
this->mAccount = mAccount;
}
void AccountHelper::updateAccount(const Account& account)
{
this->mAccount[account.getAccountId()] = account;
}
Account AccountHelper::findAccountForUser(const std::string& userId)
{
if (this->mAccount.find(userId) != this->mAccount.end())
return this->mAccount[userId];
else
return Account();
}
?????????????????????????????mock????????????????????????????????????
???????棬??????д?????????????????gmock?????????á?
????// Test case to test AccountService
????TEST(AccountServiceTest?? transferTest)
????{
????std::map<std::string?? Account> mAccount;
????mAccount["A"] = Account("A"?? 3000);
????mAccount["B"] = Account("B"?? 2000);
????AccountHelper helper(mAccount);
????MockAccountManager* pManager = new MockAccountManager();
????// specify the behavior of MockAccountManager
????// always invoke AccountHelper::findAccountForUser
????// when AccountManager::findAccountForUser is invoked
????EXPECT_CALL(*pManager?? findAccountForUser(testing::_)).WillRepeatedly(
????testing::Invoke(&helper?? &AccountHelper::findAccountForUser));
????// always invoke AccountHelper::updateAccount
????//when AccountManager::updateAccount is invoked
????EXPECT_CALL(*pManager?? updateAccount(testing::_)).WillRepeatedly(
????testing::Invoke(&helper?? &AccountHelper::updateAccount));
????AccountService as;
????// inject the MockAccountManager object into AccountService
????as.setAccountManager(pManager);
????// operate AccountService
????as.transfer("A"?? "B"?? 1005);
????// check the balance of Account("A") and Account("B") to
????//verify that AccountService has done the right job
????EXPECT_EQ(1995?? helper.findAccountForUser("A").getBalance());
????EXPECT_EQ(3005?? helper.findAccountForUser("B").getBalance());
????delete pManager;
????}
??????????????????MockAccountManager?????pManager????????????????AccountHelper???helper??EXPECT_CALL?????????????pManager?????滻???????helper?????????????????pManager??findAccountForUser?????滻??helper??findAccount????????pManager??updateAccount?????滻??helper??updateAccount??????
??????????gtest????????EXPECT_EQ???ж?????