久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<i id='QxGZH'><tr id='QxGZH'><dt id='QxGZH'><q id='QxGZH'><span id='QxGZH'><b id='QxGZH'><form id='QxGZH'><ins id='QxGZH'></ins><ul id='QxGZH'></ul><sub id='QxGZH'></sub></form><legend id='QxGZH'></legend><bdo id='QxGZH'><pre id='QxGZH'><center id='QxGZH'></center></pre></bdo></b><th id='QxGZH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='QxGZH'><tfoot id='QxGZH'></tfoot><dl id='QxGZH'><fieldset id='QxGZH'></fieldset></dl></div>
    1. <legend id='QxGZH'><style id='QxGZH'><dir id='QxGZH'><q id='QxGZH'></q></dir></style></legend>

      <small id='QxGZH'></small><noframes id='QxGZH'>

      1. <tfoot id='QxGZH'></tfoot>
          <bdo id='QxGZH'></bdo><ul id='QxGZH'></ul>
      2. 假/模擬非虛擬 C++ 方法

        fake/mock nonvirtual C++ methods(假/模擬非虛擬 C++ 方法)
          <tbody id='M71GG'></tbody>

          <bdo id='M71GG'></bdo><ul id='M71GG'></ul>
          <i id='M71GG'><tr id='M71GG'><dt id='M71GG'><q id='M71GG'><span id='M71GG'><b id='M71GG'><form id='M71GG'><ins id='M71GG'></ins><ul id='M71GG'></ul><sub id='M71GG'></sub></form><legend id='M71GG'></legend><bdo id='M71GG'><pre id='M71GG'><center id='M71GG'></center></pre></bdo></b><th id='M71GG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='M71GG'><tfoot id='M71GG'></tfoot><dl id='M71GG'><fieldset id='M71GG'></fieldset></dl></div>
            <tfoot id='M71GG'></tfoot>

            <small id='M71GG'></small><noframes id='M71GG'>

                  <legend id='M71GG'><style id='M71GG'><dir id='M71GG'><q id='M71GG'></q></dir></style></legend>

                  本文介紹了假/模擬非虛擬 C++ 方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  眾所周知,在 C++ 中模擬/偽造非虛擬測試方法是很困難的.例如,googlemock 的cookbook 有兩個建議 - 都意味著修改原始源代碼以某種方式(模板化和重寫為界面).

                  It known that in C++ mocking/faking nonvirtual methods for testing is hard. For example, cookbook of googlemock has two suggestion - both mean to modify original source code in some way (templating and rewriting as interface).

                  對于 C++ 代碼來說,這似乎是一個非常糟糕的問題.如果您無法修改需要偽造/模擬的原始代碼,如何做得最好?復制整個代碼/類(整個基類層次結構??)

                  It appear this is very bad problem for C++ code. How can be done best if you can't modify original code that needs to be faked/mocked? Duplicating whole code/class (with it whole base class hierarchy??)

                  推薦答案

                  我遵循了 Link Seam 鏈接來自 sdg 的答案.我在那里讀到了不同類型的接縫,但給我印象最深的是預處理接縫.這讓我考慮進一步利用預處理器.事實證明,可以在不實際更改調用代碼的情況下模擬任何外部依賴.

                  I followed the Link Seam link from sdg's answer. There I read about different types of seams, but I was most impressed by Preprocessing Seams. This made me think about exploiting further the preprocessor. It turned out that it is possible to mock any external dependency without actually changing the calling code.

                  為此,您必須使用替代依賴項定義編譯調用源文件.這是一個如何做的例子.

                  To do this, you have to compile the calling source file with a substitute dependency definition. Here is an example how to do it.

                  依賴.h

                  #ifndef DEPENDENCY_H
                  #define DEPENDENCY_H
                  
                  class Dependency
                  {
                  public:
                      //...
                      int foo();
                      //...
                  };
                  
                  #endif // DEPENDENCY_H
                  

                  調用者.cpp

                  #include "dependency.h"
                  
                  int bar(Dependency& dependency)
                  {
                      return dependency.foo() * 2;
                  }
                  

                  test.cpp

                  #include <assert.h>
                  
                  // block original definition
                  #define DEPENDENCY_H
                  
                  // substitute definition
                  class Dependency
                  {
                  public:
                      int foo() { return 21; }
                  };
                  
                  // include code under test
                  #include "caller.cpp"
                  
                  // the test
                  void test_bar()
                  {
                      Dependency mockDependency;
                  
                      int r = bar(mockDependency);
                  
                      assert(r == 42);
                  }
                  

                  請注意,mock 不需要實現完整的 Dependency,只需實現最小的(由 caller.cpp 使用)以便測試可以編譯和執行.通過這種方式,您可以在不更改生產代碼的情況下模擬非虛擬、靜態、全局函數或幾乎任何依賴項.我喜歡這種方法的另一個原因是與測試相關的所有內容都在一個地方.您不必到處調整編譯器和鏈接器配置.

                  Notice that the mock does not need to implement complete Dependency, just the minimum (used by caller.cpp) so the test can compile and execute. This way you can mock non-virtual, static, global functions or almost any dependency without changing the productive code. Another reason I like this approach is that everything related to the test is in one place. You don't have to tweak compiler and linker configurations here and there.

                  我已成功地將這種技術應用于具有大量依賴項的現實世界項目.我在 Include mock 中更詳細地描述了它.

                  I have applied this technique successfully on a real world project with big fat dependencies. I have described it in more detail in Include mock.

                  這篇關于假/模擬非虛擬 C++ 方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  In what ways do C++ exceptions slow down code when there are no exceptions thown?(當沒有異常時,C++ 異常會以何種方式減慢代碼速度?)
                  Why catch an exception as reference-to-const?(為什么要捕獲異常作為對 const 的引用?)
                  When and how should I use exception handling?(我應該何時以及如何使用異常處理?)
                  Scope of exception object in C++(C++中異常對象的范圍)
                  Catching exceptions from a constructor#39;s initializer list(從構造函數的初始化列表中捕獲異常)
                  Difference between C++03 throw() specifier C++11 noexcept(C++03 throw() 說明符 C++11 noexcept 之間的區別)

                      <tfoot id='KGphF'></tfoot>

                      <legend id='KGphF'><style id='KGphF'><dir id='KGphF'><q id='KGphF'></q></dir></style></legend>
                        <bdo id='KGphF'></bdo><ul id='KGphF'></ul>

                        <small id='KGphF'></small><noframes id='KGphF'>

                      • <i id='KGphF'><tr id='KGphF'><dt id='KGphF'><q id='KGphF'><span id='KGphF'><b id='KGphF'><form id='KGphF'><ins id='KGphF'></ins><ul id='KGphF'></ul><sub id='KGphF'></sub></form><legend id='KGphF'></legend><bdo id='KGphF'><pre id='KGphF'><center id='KGphF'></center></pre></bdo></b><th id='KGphF'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='KGphF'><tfoot id='KGphF'></tfoot><dl id='KGphF'><fieldset id='KGphF'></fieldset></dl></div>
                            <tbody id='KGphF'></tbody>
                            主站蜘蛛池模板: 天天操夜夜操 | 国产亚洲欧美在线视频 | 麻豆久久久久久久久久 | 可以免费观看的av | 人人九九 | 亚洲欧美一区二区三区1000 | 国产一区二区三区在线 | 欧一区 | 亚洲视频一区在线 | 成人妇女免费播放久久久 | 国产精品美女一区二区三区 | 日日骑| 青青草原精品99久久精品66 | 中文字幕第一页在线 | 精品欧美乱码久久久久久1区2区 | 久久久久久久久国产成人免费 | 夜夜夜夜草 | 免费在线看黄视频 | 亚洲精品视频在线播放 | 热re99久久精品国99热观看 | 亚洲一区二区三区四区五区中文 | 精品美女在线观看视频在线观看 | 一区二区三区四区免费视频 | 99精品久久 | 欧美激情99 | 久草网站 | 99在线免费视频 | 欧美国产视频 | 一区二区在线不卡 | 久久精品国产一区二区电影 | 日本天天操 | www.国产日本 | 伊人精品国产 | 99精品久久久国产一区二区三 | 久久久久亚洲精品 | 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 国产乱码精品一区二三赶尸艳谈 | 成人黄色电影在线播放 | 91精品欧美久久久久久久 | 天天操妹子 | 日韩一区二区在线播放 |