我有一塊C++代碼,其單元測試即將完成。例如在C++中創建存根功能
//totest.h
#include "lowerlayer.h"
class ToTest
{
LowerLayer *ll;
public:
ToTest();
void function_totest();
};
//totest.cpp
#include "totest.h"
ToTest::ToTest()
{
ll = new LowerLayer();
}
void ToTest::function_totest()
{
ll->function_lowerlayer();
}
//lowerlayer.h
class LowerLayer
{
public:
LowerLayer();
void function_lowerlayer();
};
//lowerlayer.cpp
#include "lowerlayer.h"
LowerLayer::LowerLayer()
{
//do nothing
}
void LowerLayer::function_lowerlayer()
{
//do nothing
}
這四個文件即: totest.h,totest.cpp,lowerlayer.h和lowerlayer.cpp是產品代碼文件不應該被修改爲單元測試。
我有一個測試程序,其代碼如下
//testcode.cpp
#include "totest.h"
int main()
{
ToTest *tt = new ToTest();
tt->function_totest();
//some asserts
return 0;
}
現在,我必須創建類的低層和其功能的存根功能。 當從函數function_totest調用函數function_lowerlayer時,應使用來自testcode應用程序的控制標誌來調用存根函數和實函數(一次一個)。
請提供一些建議來設計此要求。 謝謝, Ankur
存根「功能」是什麼?存根是一個尚未完成的方法的佔位符,它通常包含有關如何實現該方法的註釋,並且包含一個虛擬返回值,以便源文件將被編譯。 – 2012-02-11 07:18:31
@HunterMcMillen - 這不太正確;存根在測試環境中具有特定的含義。 [這是一篇不錯的文章](http://martinfowler.com/articles/mocksArentStubs.html)從測試的角度解釋'stubs'和'mocks'。 – dbn 2012-12-20 23:02:25
這可能是[尋找代碼存根生成器(從頭文件)](http://stackoverflow.com/q/2020568/1309332)的副本。 – dbn 2012-12-20 23:04:29