Google C++ Mocking Framework


五個月前, Google才announce C++ Testing Framwork, 現在Goolge又announce C++ Mocking Framework. 它可以support Linux, Windows 和Mac OS X. Google宣稱內部已經有超過100projects用過它, 大家的反應都還不錯, 以下是他的好處

* Simple, declarative syntax for defining mocks
* Rich set of matchers for validating function arguments
* Intuitive syntax for controlling the behavior of a mock
* Automatic verification of expectations
* Easy extensibility through new user-defined matchers and actions

以下是相關的URL:
Google C++ Mocking Framework Home Page
http://code.google.com/p/googlemock/

Documentation URL
http://code.google.com/p/googlemock/w/list

Download URL
http://code.google.com/p/googlemock/downloads/list

Google Mocking Framework Discussion Group
http://groups.google.com/group/googlemock

Google提供一個簡單的sample:
這裡有一個class ShoppingCart, 它會從server拿到tax rate.  
你要測試的狀況是: it remembers to disconnect from the server even when the server has generated an error.
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;
};

這裡demo你如何用mock server去verify
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

1. Derive the mock class from the interface. For each virtual method, count how many arguments it has, name the result n, and define it using MOCK_METHODn, whose arguments are the name and type of the method.

2. Create an instance of the mock class. It will be used where you would normally use a real object.
   
3. Set expectations on the mock object (How will it be used? What will it do?). For example, the first EXPECT_CALL says that FetchTaxRate() will be called and will return an error. The underscore (_) is a matcher that says the argument can be anything. Google Mock has many matchers you can use to precisely specify what the argument should be like. You can also define your own matcher or use an exact value.
   
4. Exercise code that uses the mock object. You'll get an error immediately if a mock method is called more times than expected or with the wrong arguments.
   
5. When the mock object is destroyed, it checks that all expectations on it have been satisfied.


Reference:
1. Announcing Google C++ Mocking Framework
http://googletesting.blogspot.com/2008/12/announcing-google-c-mocking-framework.html
2. Mockers of the (C++) World, Delight!
http://googletesting.blogspot.com/2008/12/mockers-of-c-world-delight.html
arrow
arrow
    全站熱搜

    kojenchieh 發表在 痞客邦 留言(0) 人氣()