{"id":7662,"date":"2021-12-06T22:43:52","date_gmt":"2021-12-07T06:43:52","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=7662"},"modified":"2021-12-06T22:43:52","modified_gmt":"2021-12-07T06:43:52","slug":"creating-mock-objects-with-visualgdb-and-googlemock","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/tests\/googletest\/googlemock\/","title":{"rendered":"Creating Mock Objects with VisualGDB and GoogleMock"},"content":{"rendered":"<p>This tutorial shows how to use VisualGDB with the GoogleTest\/GoogleMock framework to verify the behavior of algorithms in your code using mock classes.<\/p>\n<p>Mock classes are special implementations of interfaces that record every called method and allow checking whether it was called with expected parameters. We will demonstrate it based on a basic <strong>Math<\/strong> class containing a <strong>Square(x)<\/strong> method and a function for computing the distance between 2 vectors using the <strong>Square()<\/strong> method and the <strong>sqrt()<\/strong> function. We will show how to use RefactorScript to automatically generate mock classes for C++ interfaces so that you won&#8217;t need to do it by hand.<\/p>\n<p>Before you begin, install VisualGDB 5.6 or later and make sure you are using the Custom edition or higher.<\/p>\n<ol>\n<li>Start Visual Studio and locate the VisualGDB Linux Project Wizard under the <strong>File-&gt;New Project<\/strong> dialog:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/01-newprj-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7663\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/01-newprj-1.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>Enter the name and location for your project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/02-mock.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7664\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/02-mock.png\" alt=\"\" width=\"1024\" height=\"680\" \/><\/a><\/li>\n<li>On the <strong>Project Type<\/strong> page select &#8220;<strong>Create a new project -&gt; Unit Test -&gt; CMake<\/strong>&#8220;, then select the <strong>GoogleTest<\/strong> framework below:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/03-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7665\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/03-test.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Select the computer where you would like to build and run the test and press &#8220;<strong>Next<\/strong>&#8221; to continue:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/04-machine.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7666\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/04-machine.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>As you are using the Advanced CMake, VisualGDB can directly access the source files on the Linux machine via SSH. However, as this tutorial is <a href=\"https:\/\/github.com\/sysprogs\/tutorials\/tree\/master\/visualgdb\/Linux\/GoogleMockDemo\">checked into Git<\/a> together with the project file, we will store the sources on the Windows side and will upload them during build:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/05-src.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7667\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/05-src.png\" alt=\"\" width=\"886\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create a basic Unit Test project. Build it by pressing Ctrl-Shift-B and run the tests via Test Explorer. The <strong>FailingTest<\/strong> should immediately fail, while the other tests should succeed:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/06-tests.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7668\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/06-tests.png\" alt=\"\" width=\"1427\" height=\"912\" \/><\/a><\/li>\n<li>Now we will add a basic Vector struct. Add the following code to the main file, then press <strong>Ctrl+.<\/strong> and\u00a0 select &#8220;Run a RefactorScript -&gt; Generate a constructor&#8221;:\n<pre class=\"\">struct Vector\r\n{\r\n    double X, Y;\r\n};<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/07-ctor.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7669\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/07-ctor.png\" alt=\"\" width=\"1427\" height=\"912\" \/><\/a>VisualGDB will automatically generate a constructor for the Square class based on the existing fields:<\/p>\n<pre class=\"\">\u00a0   Vector(double x, double y)\r\n    \t\t: X(x)\r\n    \t\t, Y(y)\r\n    {\r\n    }<\/pre>\n<p>See our <a href=\"https:\/\/visualgdb.com\/tutorials\/intellisense\/refactorscript\/\">RefactorScript tutorial<\/a> for more details about generating constructors.<\/li>\n<li>Now we will add the following code to the program:\n<ul>\n<li>A <strong>Math<\/strong> class providing the <strong>Square()<\/strong> method.<\/li>\n<li>A <strong>GetDistance()<\/strong> function computing the distance between 2 vectors using the <strong>Square()<\/strong> method.<\/li>\n<li>A <strong>SquareTest()<\/strong> unit test checking that <strong>Square()<\/strong> function returns expected results for a single pair of inputs<\/li>\n<\/ul>\n<p>Add the following code to your main source file:<\/p>\n<pre class=\"\">class Math\r\n{\r\npublic:\r\n  double Square(double arg)\r\n  {\r\n      return arg * arg;\r\n  }\r\n};\r\n\r\ndouble GetDistance(Math *pMath, const Vector &amp;a, const Vector &amp;b)\r\n{\r\n    return sqrt(pMath-&gt;Square(a.X - b.X) + pMath-&gt;Square(a.Y - b.Y));\r\n}\r\n\r\nTEST(DemoTestGroup, SquareTest)\r\n{\r\n    Vector left(0, 0);\r\n    Vector right(3, 4);\r\n    Math math;\r\n    double distance = GetDistance(&amp;math, left, right);\r\n    EXPECT_EQ(distance, 5);\r\n}<\/pre>\n<\/li>\n<li>Build the project and run the test, making sure it succeeds:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/09-iface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7670\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/09-iface.png\" alt=\"\" width=\"1427\" height=\"912\" \/><\/a><\/li>\n<li>The current version of the test checks that the <strong>GetDistance()<\/strong> function returned the correct result, but it doesn&#8217;t check how many times it calls the <strong>Square()<\/strong> method and with what arguments. We will now do exactly that &#8211; replace the real <strong>Math<\/strong> class with a <strong>Mock<\/strong> class that counts how many times Square() was called. However, before we can do that, Math needs to be refactored into the <strong>IMath<\/strong> interface and a <strong>Math<\/strong> class implementing it:\n<pre class=\"\">class IMath\r\n{\r\npublic:\r\n  virtual double Square(double arg) = 0;\r\n};\r\n\r\nclass Math : public IMath\r\n{\r\npublic:\r\n  double Square(double arg) override\r\n  {\r\n      return arg * arg;\r\n  }\r\n};<\/pre>\n<\/li>\n<li>Go to the <strong>IMath<\/strong> class, press &#8220;Ctrl+.&#8221; and select &#8220;Run a RefactorScript -&gt; Generate a mock class&#8221;: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/10-genmock.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7671\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/10-genmock.png\" alt=\"\" width=\"1427\" height=\"912\" \/><\/a><\/li>\n<li>VisualGDB will automatically generate a mock class for the <strong>IMath<\/strong> interface using the <strong>GoogleMock<\/strong> syntax: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/11-gen.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7672\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/11-gen.png\" alt=\"\" width=\"1045\" height=\"694\" \/><\/a>Press &#8220;Copy and close&#8221;, insert the generated mock class in the main source file and update it to inherit the <strong>IMath<\/strong> interface.<\/li>\n<li>Finally, include the <strong>&lt;gmock\/gmock.h&gt;<\/strong> file and update the <strong>SquareTest<\/strong> unit test as shown below:\n<pre class=\"\">TEST(DemoTestGroup, SquareTest)\r\n{\r\n    Vector left(0, 0);\r\n    Vector right(3, 4);\r\n    MockIMath math;\r\n\r\n    EXPECT_CALL(math, Square(3)).WillOnce(Return(9));\r\n    EXPECT_CALL(math, Square(4)).WillOnce(Return(16));\r\n\r\n    double distance = GetDistance(&amp;math, left, right);\r\n    EXPECT_EQ(distance, 5);\r\n}<\/pre>\n<p>Note that <strong>EXPECT_CALL()<\/strong> macros. They configure the <strong>MockIMath<\/strong> instance to verify that it&#8217;s being called with certain arguments, and to return the specified values. You can read more about GoogleMock syntax on <a href=\"https:\/\/google.github.io\/googletest\/gmock_cook_book.html\">this page<\/a>.<\/li>\n<li>Run the test. It will now fail with a detailed message explaining that <strong>Square()<\/strong> was called the <strong>arg=-3<\/strong> that doesn&#8217;t match neither <strong>3<\/strong> nor <strong>4<\/strong>:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/12-fail.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7673\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/12-fail.png\" alt=\"\" width=\"1427\" height=\"912\" \/><\/a><\/li>\n<li>Update the <strong>EXPECT_CALL()<\/strong> macros to expect the values of <strong>-3<\/strong> and <strong>-4<\/strong> respectively. The test will now pass:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/13-pass.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7674\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/13-pass.png\" alt=\"\" width=\"1427\" height=\"912\" \/><\/a><\/li>\n<li>You can tweak the code produced by RefactorScripts by directly editing the script. VisualGDB will immediately update the output. E.g. change first 2 lines of the <strong>DoGenerateMockClass()<\/strong> generator to automatically strip &#8220;I&#8221; from the class name and to inherit the interface:\n<pre class=\"\">set strippedName = class.ShortName.StripPrefix(\"I\");\r\n&gt;class Mock$strippedName : public $class.QualifiedName<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/14-strip.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7675\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/14-strip.png\" alt=\"\" width=\"1045\" height=\"694\" \/><\/a><\/li>\n<li>The <strong>GenerateMockClass<\/strong> script can automatically append the <strong>const<\/strong> qualifier for the const methods, however it won&#8217;t automatically add the <strong>override<\/strong> keyword. To change this, locate the logic for outputting &#8220;(const)&#8221; near the end of the generator and replace it with the following:\n<pre class=\"\">if (method.IsConst || method.IsVirtual)\r\n{\r\n\t&gt;&gt;, (\r\n\tif (method.IsConst)\r\n\t\t&gt;&gt;const\r\n\tif (method.IsConst &amp;&amp; method.IsVirtual)\r\n\t\t&gt;&gt;, \r\n\tif (method.IsVirtual)\r\n\t\t&gt;&gt;override\r\n\t&gt;&gt;)\r\n}<\/pre>\n<p>Now all virtual methods will be marked as &#8220;(override)&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/15-override-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7678\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/15-override-1.png\" alt=\"\" width=\"1045\" height=\"694\" \/><\/a><\/li>\n<li>Press &#8220;Copy and close&#8221; to copy the updated mock class into the Clipboard so that you can replace the original implementation:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/16-mock2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7677\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2021\/11\/16-mock2.png\" alt=\"\" width=\"1427\" height=\"912\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use VisualGDB with the GoogleTest\/GoogleMock framework to verify the behavior of algorithms in your code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128],"tags":[52,240,238,129],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7662"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=7662"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7662\/revisions"}],"predecessor-version":[{"id":7679,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/7662\/revisions\/7679"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=7662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=7662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=7662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}