Google已经开源了自己的C++ Mocking 框架
程序员文章站
2022-05-20 18:27:44
...
Google在开源了自己的C++测试框架几个月后,又开源了Google C++ Mocking 框架(Google Mock),使用BSD许可。
Google Mock在google内部超过100个项目中使用过,由Zhanyong Wan,一个google的软件工程师(看名字大概是华裔),从jMock和EasyMock中获得灵感而开发。
这个框架能够在Linux,Windows 和Mac OS X上使用。
Zhanyong 提供了mocking example范例:
class TaxServer { // Returns the tax rate of a location (by postal code) or -1 on error. virtual double FetchTaxRate( const string& postal_code) = 0; virtual void CloseConnection() = 0; }; class MockTaxServer : public TaxServer { // #1 MOCK_METHOD1(FetchTaxRate, double(const string&)); MOCK_METHOD0(CloseConnection, void()); }; TEST(ShoppingCartTest, StillCallsCloseIfServerErrorOccurs) { MockTaxServer mock_taxserver; // #2 EXPECT_CALL(mock_taxserver, FetchTaxRate(_)). WillOnce(Return(-1)); // #3 EXPECT_CALL(mock_taxserver, CloseConnection()); ShoppingCart cart(&mock_taxserver); // #4 cart.CalculateTax(); // Calls FetchTaxRate() // and CloseConnection(). } // #5
详细内容阅读:Google Has Open-Sourced Their C++ Mocking Framework
更多有用的链接: Home Page. Complete documentation. Binaries download. Google Mock for Dummies 这是一个google mock的快速引导和范例