{"id":6005,"date":"2020-03-24T17:22:31","date_gmt":"2020-03-24T17:22:31","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=6005"},"modified":"2024-02-16T10:28:50","modified_gmt":"2024-02-16T10:28:50","slug":"testing-example-2-mock","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=6005","title":{"rendered":"Unit testing series &#8211; Test with Mock (Part 5)"},"content":{"rendered":"<p>The third szenario is using a mock.<\/p>\n<hr>\n<p><strong>Mock<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: WindowManagerMock.hpp; notranslate\" title=\"WindowManagerMock.hpp\">\r\n#pragma once\r\n#include &quot;WindowManager.hpp&quot;\r\n#include &lt;gmock\/gmock.h&gt;\r\n\r\nclass WindowManagerMock : public IWindowManager\r\n{\r\npublic:\r\n  MOCK_METHOD4(drawAt, bool(unsigned startX, unsigned endX,\r\n                            unsigned startY, unsigned endY));\r\n};\r\n\r\n<\/pre>\n<hr>\n<p><strong>Test<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: UserInterfaceTest.hpp; notranslate\" title=\"UserInterfaceTest.hpp\">\r\n#pragma once\r\n#include &quot;WindowManager.hpp&quot;\r\n#include &quot;UserInterface.hpp&quot;\r\n#include &lt;gtest\/gtest.h&gt;\r\n#include &lt;memory&gt;\r\n\r\nclass UserInterfaceTest : public testing::Test\r\n{\r\nprotected:\r\n  virtual void SetUp();\r\n  virtual void TearDown();\r\n  std::shared_ptr&lt;IWindowManager&gt; m_windowMgr;\r\n  std::unique_ptr&lt;UserInterface&gt; m_userInter;\r\n};\r\n<\/pre>\n<pre class=\"brush: cpp; gutter: false; title: UserInterfaceTest.cpp; notranslate\" title=\"UserInterfaceTest.cpp\">\r\n#include &quot;UserInterfaceTest.hpp&quot;\r\n#include &quot;WindowManagerMock.hpp&quot;\r\n\r\nvoid UserInterfaceTest::SetUp()\r\n{\r\n  m_windowMgr.reset(new WindowManagerMock());\r\n  m_userInter.reset(new UserInterface(m_windowMgr));\r\n}\r\n\r\nvoid UserInterfaceTest::TearDown() {}\r\n\r\nTEST_F(UserInterfaceTest, loadWindow)\r\n{\r\n  std::shared_ptr&lt;WindowManagerMock&gt; windowMgrMock\r\n    = std::dynamic_pointer_cast&lt;WindowManagerMock&gt;(m_windowMgr);\r\n  ASSERT_NE(windowMgrMock, nullptr);\r\n\r\n  using namespace testing;\r\n  EXPECT_CALL(*windowMgrMock, drawAt(_,_,_,_)).WillRepeatedly(Return(true));\r\n  bool done = m_userInter-&gt;loadWindow(0, 100, 0, 100);\r\n  EXPECT_EQ(done, true);\r\n}\r\n<\/pre>\n<hr>\n<p><strong>App<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: Main.Test.cpp; notranslate\" title=\"Main.Test.cpp\">\r\n#include &lt;gtest\/gtest.h&gt;\r\n\r\nint main(int argc, char **argv)\r\n{\r\n  testing::InitGoogleTest(&amp;argc, argv);\r\n  return RUN_ALL_TESTS();\r\n}\r\n<\/pre>\n<hr>\n<p><strong>Exec<\/strong><\/p>\n<pre>\r\n$ g++ -c UserInterface.cpp WindowManager.cpp\r\n$ ar rcs libwnd.a UserInterface.o WindowManager.o\r\n$ g++ UserInterfaceTest.cpp Main.Test.cpp libwnd.a -lpthread -lgtest -lgmock\r\n$ .\/a.out \r\n[==========] Running 1 test from 1 test case.\r\n[----------] Global test environment set-up.\r\n[----------] 1 test from UserInterfaceTest\r\n[ RUN      ] UserInterfaceTest.loadWindow\r\n[       OK ] UserInterfaceTest.loadWindow (0 ms)\r\n[----------] 1 test from UserInterfaceTest (1 ms total)\r\n\r\n[----------] Global test environment tear-down\r\n[==========] 1 test from 1 test case ran. (1 ms total)\r\n[  PASSED  ] 1 test.\r\n<\/pre>\n<hr>\n<ul>\n<li>Prev: <a href=\"http:\/\/www.max-sperling.bplaced.net\/?p=858\">Unit testing series \u2013 Test with Stub (Part 4)<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The third szenario is using a mock. Mock Test App Exec $ g++ -c UserInterface.cpp WindowManager.cpp $ ar rcs libwnd.a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[33],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/6005"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6005"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/6005\/revisions"}],"predecessor-version":[{"id":16767,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/6005\/revisions\/16767"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6005"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}