02 Stubs and Mocks
02 Stubs and Mocks
02 Stubs and Mocks
书商能够提供多种图书 ( 附有价格 ) 供管
理员进行选择
管理员仅会挑选当前图书馆中不存在的书
籍进行购买
管理员选好图书后 , 会向书商支付所选书
籍的总金额
2
EXERCISE 从原始 Repo 上更新代码
git remote add source https://github.com/
coney/cpp-unit-testing.git
git pull source master
思考如何编写 Librarian::store(vendor)
的测试
3
MOCKING
With Google Mock
4
WHY GOOGLE MOCK
5
MOCK OBJECT
6
EXPECTATIONS
7
ACTIONS
8
USING GMOCK
include <gmock/gmock.h>
write your mocks and tests
initialize gmock by InitGoogleMock()
call RUN_ALL_TESTS() in main() function
compile and run
书商能够提供多种图书 ( 附有价格 ) 供管
理员进行选择
管理员仅会挑选当前图书馆中不存在的书
籍进行购买
管理员选好图书后 , 会向书商支付所选书
籍的总金额
1
0
STUBS VS MOCKS
Behavior Based Verification
1
1
TEST DOUBLES
1
3
CARDINALITIES
1
4
EXERCISE 图书馆管理员 (Librarian) 会定期向书商
(BookVendor) 购买一些书籍来充实图书馆 :
书商能够提供多种图书 ( 附有价格 ) 供管
理员进行选择
管理员仅会挑选当前图书馆中不存在的书
籍进行购买
管理员选好图书后 , 会向书商支付所选书
籍的总金额
1
5
REFERENCE
https://
github.com/coney/cpp-unit-testing/tree/master/02-
GMock
https://
code.google.com/p/googlemock/wiki/ForDummies
https://
code.google.com/p/googlemock/wiki/CheatSheet
https://
code.google.com/p/googlemock/wiki/CookBook
https://
code.google.com/p/googlemock/wiki/FrequentlyAsk
1
6
THANK YOU
For questions or suggestions:
Wu Kun
kunwu@thoughtworks.com