Testing on the Toilet

December 11, 2008

Mockers of the (C++) World, Delight! Life is unfair. You work every bit as hard as Joe the Java programmer next to you. Yet as a C++ programmer, you don't get to play with all the fancy programming tools Joe takes for granted. In particular, without a good mocking framework, mock objects in C++ have to be rolled by hand. Boy, is that tedious! (Not to mention how error-prone it is.) Why should you endure this? Dread no more. Google Mock is finally here to help! It's a Google-originated open-source framework for creating and using C++ mocks. Inspired by jMock and EasyMock, Google Mock is easy to use, yet flexible and extensible. All you need to get started is the ability to count from 0 to 10 and use an editor. Think you can do it? Let's try this simple example: you have a ShoppingCart class that gets the tax rate from a server, and you want to test that it remembers to disconnect from the server even when the server has generated an error. It's easy to write the test using a mock tax server, which implements this interface: 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; };

Here's how you mock it and use the mock server to verify the expected behavior of ShoppingCart: class MockTaxServer : public TaxServer { MOCK_METHOD1(FetchTaxRate, double(const string&)); MOCK_METHOD0(CloseConnection, void()); };

// #1

TEST(ShoppingCartTest, StillCallsCloseIfServerErrorOccurs) { MockTaxServer mock_taxserver; // EXPECT_CALL(mock_taxserver, FetchTaxRate(_)).WillOnce(Return(-1)); // EXPECT_CALL(mock_taxserver, CloseConnection()); ShoppingCart cart(&mock_taxserver); // cart.CalculateTax(); // Calls FetchTaxRate() and CloseConnection(). } //

#2 #3 #4 #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. You can also use Google Mock for rapid prototyping – and get a better design. To find out more, visit the project homepage at http://code.google.com/p/googlemock/. Now, be the first one on your block to use Google Mock and prepare to be envied. Did I say life is unfair?

More information, discussion, and archives: http://googletesting.blogspot.com Copyright © 2008 Google, Inc. Licensed under a Creative Commons Attribution–ShareAlike 2.5 License (http://creativecommons.org/licenses/by-sa/2.5/).

C++ Code

Dec 11, 2008 - It's easy to write the test using a mock tax server, which implements this interface: class TaxServer {. // Returns the tax rate of a location (by postal code) or -1 on error. ... life is unfair? More information, discussion, and archives:.

159KB Sizes 17 Downloads 197 Views

Recommend Documents

Finding Data Races in C++ Code
If you've got some multi-threaded code, you may have data races in it. Data races ... Valgrind is a binary translation framework which has other useful tools such.

neet-code-c-answer-key.pdf
May 14, 2017 - 36. (3). 37. (4). 38. (4). 39. (1). 40. (1). 41. (2). 42. (1). 43. (4). 44. (4). 45. (3) ... BOOKLET CODE - C ... Displaying neet-code-c-answer-key.pdf.

neet-code-c-question-paper.pdf
Which of the following is made up of dead cells ? (1) Phloem. (2) Xylemparenchyma. (3) Collenchyma. (4) Phellem ... (b) They are somatic cells. (c) They do not metabolize. (d) Alltheirinternal space is availableforo)^gen ... i ii -k^ i i II. IP. fl.

c. Dutch Code Civil Procedure.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. c. Dutch Code Civil Procedure.pdf. c. Dutch Code Civil Procedure.pdf. Open. Extract. Open with. Sign In. Mai

pdf The Robert C. Martin Clean Code Collection ...
In Clean Code, legendary software expert Robert C. Martin has teamed up with his colleagues ... How to get into the flow of coding and get past writer's block.

Microsoft Press - Adaptive Code via C#.pdf
From the Library of Ida Schander. www.it-ebooks.info. Page 3 of 433. Microsoft Press - Adaptive Code via C#.pdf. Microsoft Press - Adaptive Code via C#.pdf.

Course Code : CS-62 Course Title : 'C' Programming & Data ...
If you have any query just email us at www.ignoufriend.blogspot.com Email : [email protected]. Course Code : CS-62. Course Title : 'C' Programming ...