[{"data":1,"prerenderedAt":951},["ShallowReactive",2],{"doc-progmasoft-mocking":3},{"id":4,"title":5,"body":6,"description":942,"extension":943,"meta":944,"navigation":56,"path":945,"seo":946,"slug":947,"source":948,"stem":949,"__hash__":950},"docs\u002Fdocs\u002Fprogmasoft-mocking.md","Mocking",{"type":7,"value":8,"toc":935},"minimark",[9,13,21,412,419,426,431,457,460,515,525,529,535,601,616,623,713,726,732,736,743,798,801,805,816,911,917,921,928,931],[10,11,5],"h1",{"id":12},"mocking",[14,15,16,20],"p",{},[17,18,19],"code",{},"\u003CProgmasoft\u002FCatch3\u002FMock.hpp>"," replaces a dependency of the code under test\nwith a mock that checks how it is called. Mocks report through Catch's\nassertion path: an unexpected call, a call out of order and an unmet\nexpectation fail the test case that owns the mock, at the line that declared\nthe expectation, in every report the runner writes.",[22,23,28],"pre",{"className":24,"code":25,"language":26,"meta":27,"style":27},"language-cpp shiki shiki-themes github-dark","#include \u003CProgmasoft\u002FCatch3\u002FMock.hpp>\n#include \u003CProgmasoft\u002FCatch3\u002FTestMacros.hpp>\n\nclass Store\n{\npublic:\n    virtual ~Store() = default;\n    virtual int Get(const std::string &key) const = 0;\n    virtual void Put(const std::string &key, int value) = 0;\n};\n\nclass MockStore final : public Store\n{\npublic:\n    CATCH3_MOCK_METHOD(int, Get, (const std::string &), const override);\n    CATCH3_MOCK_METHOD(void, Put, (const std::string &, int), override);\n};\n\nTEST_CASE(\"A score goes up by one\")\n{\n    MockStore store;\n    CATCH3_EXPECT_CALL(store.GetMock)(\"ada\").Returns(41);\n    CATCH3_EXPECT_CALL(store.PutMock)(\"ada\", 42);\n\n    AddPoint(store, \"ada\");\n}\n","cpp","",[17,29,30,43,51,58,68,75,81,102,149,191,197,202,221,226,231,270,308,313,318,332,337,346,371,388,393,406],{"__ignoreMap":27},[31,32,35,39],"span",{"class":33,"line":34},"line",1,[31,36,38],{"class":37},"snl16","#include",[31,40,42],{"class":41},"sU2Wk"," \u003CProgmasoft\u002FCatch3\u002FMock.hpp>\n",[31,44,46,48],{"class":33,"line":45},2,[31,47,38],{"class":37},[31,49,50],{"class":41}," \u003CProgmasoft\u002FCatch3\u002FTestMacros.hpp>\n",[31,52,54],{"class":33,"line":53},3,[31,55,57],{"emptyLinePlaceholder":56},true,"\n",[31,59,61,64],{"class":33,"line":60},4,[31,62,63],{"class":37},"class",[31,65,67],{"class":66},"svObZ"," Store\n",[31,69,71],{"class":33,"line":70},5,[31,72,74],{"class":73},"s95oV","{\n",[31,76,78],{"class":33,"line":77},6,[31,79,80],{"class":37},"public:\n",[31,82,84,87,90,93,96,99],{"class":33,"line":83},7,[31,85,86],{"class":37},"    virtual",[31,88,89],{"class":66}," ~Store",[31,91,92],{"class":73},"() ",[31,94,95],{"class":37},"=",[31,97,98],{"class":37}," default",[31,100,101],{"class":73},";\n",[31,103,105,107,110,113,116,119,122,125,128,131,135,138,140,143,147],{"class":33,"line":104},8,[31,106,86],{"class":37},[31,108,109],{"class":37}," int",[31,111,112],{"class":66}," Get",[31,114,115],{"class":73},"(",[31,117,118],{"class":37},"const",[31,120,121],{"class":66}," std",[31,123,124],{"class":73},"::",[31,126,127],{"class":66},"string",[31,129,130],{"class":37}," &",[31,132,134],{"class":133},"s9osk","key",[31,136,137],{"class":73},") ",[31,139,118],{"class":37},[31,141,142],{"class":37}," =",[31,144,146],{"class":145},"sDLfK"," 0",[31,148,101],{"class":73},[31,150,152,154,157,160,162,164,166,168,170,172,174,177,180,183,185,187,189],{"class":33,"line":151},9,[31,153,86],{"class":37},[31,155,156],{"class":37}," void",[31,158,159],{"class":66}," Put",[31,161,115],{"class":73},[31,163,118],{"class":37},[31,165,121],{"class":66},[31,167,124],{"class":73},[31,169,127],{"class":66},[31,171,130],{"class":37},[31,173,134],{"class":133},[31,175,176],{"class":73},", ",[31,178,179],{"class":37},"int",[31,181,182],{"class":133}," value",[31,184,137],{"class":73},[31,186,95],{"class":37},[31,188,146],{"class":145},[31,190,101],{"class":73},[31,192,194],{"class":33,"line":193},10,[31,195,196],{"class":73},"};\n",[31,198,200],{"class":33,"line":199},11,[31,201,57],{"emptyLinePlaceholder":56},[31,203,205,207,210,213,216,219],{"class":33,"line":204},12,[31,206,63],{"class":37},[31,208,209],{"class":66}," MockStore",[31,211,212],{"class":37}," final",[31,214,215],{"class":73}," : ",[31,217,218],{"class":37},"public",[31,220,67],{"class":66},[31,222,224],{"class":33,"line":223},13,[31,225,74],{"class":73},[31,227,229],{"class":33,"line":228},14,[31,230,80],{"class":37},[31,232,234,237,239,241,243,246,249,251,253,255,257,259,262,264,267],{"class":33,"line":233},15,[31,235,236],{"class":66},"    CATCH3_MOCK_METHOD",[31,238,115],{"class":73},[31,240,179],{"class":37},[31,242,176],{"class":73},[31,244,245],{"class":66},"Get",[31,247,248],{"class":73},", (",[31,250,118],{"class":37},[31,252,121],{"class":66},[31,254,124],{"class":73},[31,256,127],{"class":73},[31,258,130],{"class":37},[31,260,261],{"class":73},"), ",[31,263,118],{"class":37},[31,265,266],{"class":66}," override",[31,268,269],{"class":73},");\n",[31,271,273,275,277,280,282,285,287,289,291,293,295,297,299,301,303,306],{"class":33,"line":272},16,[31,274,236],{"class":66},[31,276,115],{"class":73},[31,278,279],{"class":37},"void",[31,281,176],{"class":73},[31,283,284],{"class":66},"Put",[31,286,248],{"class":73},[31,288,118],{"class":37},[31,290,121],{"class":66},[31,292,124],{"class":73},[31,294,127],{"class":73},[31,296,130],{"class":37},[31,298,176],{"class":73},[31,300,179],{"class":37},[31,302,261],{"class":73},[31,304,305],{"class":66},"override",[31,307,269],{"class":73},[31,309,311],{"class":33,"line":310},17,[31,312,196],{"class":73},[31,314,316],{"class":33,"line":315},18,[31,317,57],{"emptyLinePlaceholder":56},[31,319,321,324,326,329],{"class":33,"line":320},19,[31,322,323],{"class":66},"TEST_CASE",[31,325,115],{"class":73},[31,327,328],{"class":41},"\"A score goes up by one\"",[31,330,331],{"class":73},")\n",[31,333,335],{"class":33,"line":334},20,[31,336,74],{"class":73},[31,338,340,343],{"class":33,"line":339},21,[31,341,342],{"class":66},"    MockStore",[31,344,345],{"class":73}," store;\n",[31,347,349,352,355,358,361,364,366,369],{"class":33,"line":348},22,[31,350,351],{"class":66},"    CATCH3_EXPECT_CALL",[31,353,354],{"class":73},"(store.GetMock)(",[31,356,357],{"class":41},"\"ada\"",[31,359,360],{"class":73},").",[31,362,363],{"class":66},"Returns",[31,365,115],{"class":73},[31,367,368],{"class":145},"41",[31,370,269],{"class":73},[31,372,374,376,379,381,383,386],{"class":33,"line":373},23,[31,375,351],{"class":66},[31,377,378],{"class":73},"(store.PutMock)(",[31,380,357],{"class":41},[31,382,176],{"class":73},[31,384,385],{"class":145},"42",[31,387,269],{"class":73},[31,389,391],{"class":33,"line":390},24,[31,392,57],{"emptyLinePlaceholder":56},[31,394,396,399,402,404],{"class":33,"line":395},25,[31,397,398],{"class":66},"    AddPoint",[31,400,401],{"class":73},"(store, ",[31,403,357],{"class":41},[31,405,269],{"class":73},[31,407,409],{"class":33,"line":408},26,[31,410,411],{"class":73},"}\n",[14,413,414,415,418],{},"A mock verifies itself in its destructor, so nothing has to be called at the\nend of the test case. When the test case is already being left by a failure or\na skip, the expectations it did not reach are not reported on top of it.\n",[17,416,417],{},"Verify()"," checks the expectations earlier and forgets them, for a test case\nthat goes on with new ones.",[14,420,421,422,425],{},"With the amalgamated files, ",[17,423,424],{},"catch_amalgamated.hpp"," holds all of this.",[427,428,430],"h2",{"id":429},"declaring-mocks","Declaring mocks",[14,432,433,436,437,440,441,444,445,176,447,176,450,452,453,456],{},[17,434,435],{},"CATCH3_MOCK_METHOD(result, name, (types), specifiers)"," declares the member\nfunction ",[17,438,439],{},"name"," and the mock ",[17,442,443],{},"nameMock"," beside it. The specifiers are whatever\nfollows the parameters: ",[17,446,118],{},[17,448,449],{},"noexcept",[17,451,305],{},". Up to 8 parameters are\nsupported, and a type with a comma in it, such as ",[17,454,455],{},"std::map\u003Cstd::string, int>",",\nneeds an alias.",[14,458,459],{},"Overloads need mocks with names of their own:",[22,461,463],{"className":24,"code":462,"language":26,"meta":27,"style":27},"CATCH3_MOCK_OVERLOAD(void, Put, PutOne, (const std::string &, int), override);\nCATCH3_MOCK_OVERLOAD(void, Put, PutAll, (const Scores &), override);\n",[17,464,465,494],{"__ignoreMap":27},[31,466,467,470,472,474,477,479,481,483,485,487,489,491],{"class":33,"line":34},[31,468,469],{"class":66},"CATCH3_MOCK_OVERLOAD",[31,471,115],{"class":73},[31,473,279],{"class":37},[31,475,476],{"class":73},", Put, PutOne, (",[31,478,118],{"class":37},[31,480,121],{"class":66},[31,482,124],{"class":73},[31,484,127],{"class":73},[31,486,130],{"class":37},[31,488,176],{"class":73},[31,490,179],{"class":37},[31,492,493],{"class":73},"), override);\n",[31,495,496,498,500,502,505,507,510,513],{"class":33,"line":45},[31,497,469],{"class":66},[31,499,115],{"class":73},[31,501,279],{"class":37},[31,503,504],{"class":73},", Put, PutAll, (",[31,506,118],{"class":37},[31,508,509],{"class":73}," Scores ",[31,511,512],{"class":37},"&",[31,514,493],{"class":73},[14,516,517,520,521,524],{},[17,518,519],{},"CATCH3_MOCK_FUNCTION(name, signature)"," declares a ",[17,522,523],{},"MockFunction"," on its own.",[427,526,528],{"id":527},"setting-expectations","Setting expectations",[14,530,531,534],{},[17,532,533],{},"CATCH3_EXPECT_CALL(mock)(arguments...)"," expects one call. Without arguments it\naccepts any arguments; otherwise each parameter takes one of these:",[536,537,538,551],"table",{},[539,540,541],"thead",{},[542,543,544,548],"tr",{},[545,546,547],"th",{},"Written as",[545,549,550],{},"Accepts",[552,553,554,570,581,591],"tbody",{},[542,555,556,560],{},[557,558,559],"td",{},"a value",[557,561,562,563,566,567],{},"what compares equal to it; strings compare by value, also as ",[17,564,565],{},"const char *",", and integers by value whatever their types, so 256 never accepts an ",[17,568,569],{},"unsigned char",[542,571,572,578],{},[557,573,574,575],{},"a Catch matcher, such as ",[17,576,577],{},"StartsWith(\"a\")",[557,579,580],{},"what the matcher accepts",[542,582,583,588],{},[557,584,585],{},[17,586,587],{},"Any",[557,589,590],{},"anything",[542,592,593,598],{},[557,594,595],{},[17,596,597],{},"Where(predicate, \"description\")",[557,599,600],{},"what the predicate accepts",[14,602,603,604,607,608,611,612,615],{},"A composition such as ",[17,605,606],{},"StartsWith(\"a\") && EndsWith(\"z\")"," refers to temporaries\nthat are gone when the mock is called, and Catch's vector matchers, such as\n",[17,609,610],{},"Equals(vector)",", refer to their vector. Neither compiles here; put them in a\n",[17,613,614],{},"Where",". A matcher of your own has to hold what it compares with.",[14,617,618,619,622],{},"The result of ",[17,620,621],{},"CATCH3_EXPECT_CALL"," takes these, in any order:",[536,624,625,635],{},[539,626,627],{},[542,628,629,632],{},[545,630,631],{},"Member",[545,633,634],{},"Meaning",[552,636,637,662,675,689,703],{},[542,638,639,659],{},[557,640,641,176,644,176,647,176,650,176,653,176,656],{},[17,642,643],{},"Times(n)",[17,645,646],{},"Between(a, b)",[17,648,649],{},"AtLeast(n)",[17,651,652],{},"AtMost(n)",[17,654,655],{},"AnyTimes()",[17,657,658],{},"Never()",[557,660,661],{},"how many calls it wants; one by default",[542,663,664,669],{},[557,665,666],{},[17,667,668],{},"Returns(value)",[557,670,671,672],{},"the result of each call; a type that cannot be copied moves out on the first one, and a reference result refers to ",[17,673,674],{},"value",[542,676,677,682],{},[557,678,679],{},[17,680,681],{},"Does(function)",[557,683,684,685,688],{},"runs ",[17,686,687],{},"function"," with the arguments, and returns what it returns; it keeps its state between calls",[542,690,691,696],{},[557,692,693],{},[17,694,695],{},"Throws(exception)",[557,697,698,699,702],{},"throws ",[17,700,701],{},"exception"," from each call",[542,704,705,710],{},[557,706,707],{},[17,708,709],{},"InSequence(sequence)",[557,711,712],{},"see below",[14,714,715,716,718,719,722,723,725],{},"A call goes to the first expectation, in the order they were declared, that\naccepts its arguments and can take another call. A call none of them accepts\nfails the test case and returns a value-initialized result. So does a call they\naccept when all of them are used up. When the result cannot be made that way, a\nreference for example, the test case ends there; the same happens to an\nexpectation with no ",[17,717,363],{}," or ",[17,720,721],{},"Does",". A ",[17,724,449],{}," function cannot leave the\ntest case that way, so such a call ends the process.",[14,727,728,729,731],{},"A mock with no expectation for a call treats it as a failure. An expectation\nwith ",[17,730,655],{}," is how to allow calls that do not matter to the test.",[427,733,735],{"id":734},"sequences","Sequences",[14,737,738,739,742],{},"Expectations in a ",[17,740,741],{},"Sequence"," have to be met in the order they were declared in,\nacross mocks:",[22,744,746],{"className":24,"code":745,"language":26,"meta":27,"style":27},"Sequence order;\nCATCH3_EXPECT_CALL(file.OpenMock)().InSequence(order);\nCATCH3_EXPECT_CALL(file.WriteMock)(Any).AtLeast(1).InSequence(order);\nCATCH3_EXPECT_CALL(log.FlushMock)().InSequence(order);\n",[17,747,748,753,766,787],{"__ignoreMap":27},[31,749,750],{"class":33,"line":34},[31,751,752],{"class":73},"Sequence order;\n",[31,754,755,757,760,763],{"class":33,"line":45},[31,756,621],{"class":66},[31,758,759],{"class":73},"(file.OpenMock)().",[31,761,762],{"class":66},"InSequence",[31,764,765],{"class":73},"(order);\n",[31,767,768,770,773,776,778,781,783,785],{"class":33,"line":53},[31,769,621],{"class":66},[31,771,772],{"class":73},"(file.WriteMock)(Any).",[31,774,775],{"class":66},"AtLeast",[31,777,115],{"class":73},[31,779,780],{"class":145},"1",[31,782,360],{"class":73},[31,784,762],{"class":66},[31,786,765],{"class":73},[31,788,789,791,794,796],{"class":33,"line":60},[31,790,621],{"class":66},[31,792,793],{"class":73},"(log.FlushMock)().",[31,795,762],{"class":66},[31,797,765],{"class":73},[14,799,800],{},"A step that is passed takes no more calls. A step reached before the ones ahead\nof it have their calls fails the test case and names them.",[427,802,804],{"id":803},"free-functions","Free functions",[14,806,807,808,811,812,815],{},"Code that reaches a free function through a seam, a ",[17,809,810],{},"std::function"," or a\nfunction pointer it calls through, can be pointed at a mock. ",[17,813,814],{},"ScopedSeam"," puts\nthe seam back at the end of its scope:",[22,817,819],{"className":24,"code":818,"language":26,"meta":27,"style":27},"CATCH3_MOCK_FUNCTION(clock, long());\nCATCH3_EXPECT_CALL(clock)().Returns(50L);\n\nconst ScopedSeam seam(clockSeam, clock.AsFunction());\nCHECK(SecondsSince(20) == 30);\n",[17,820,821,835,854,858,886],{"__ignoreMap":27},[31,822,823,826,829,832],{"class":33,"line":34},[31,824,825],{"class":66},"CATCH3_MOCK_FUNCTION",[31,827,828],{"class":73},"(clock, ",[31,830,831],{"class":37},"long",[31,833,834],{"class":73},"());\n",[31,836,837,839,842,844,846,849,852],{"class":33,"line":45},[31,838,621],{"class":66},[31,840,841],{"class":73},"(clock)().",[31,843,363],{"class":66},[31,845,115],{"class":73},[31,847,848],{"class":145},"50",[31,850,851],{"class":37},"L",[31,853,269],{"class":73},[31,855,856],{"class":33,"line":53},[31,857,57],{"emptyLinePlaceholder":56},[31,859,860,862,865,868,870,873,875,878,881,884],{"class":33,"line":60},[31,861,118],{"class":37},[31,863,864],{"class":66}," ScopedSeam",[31,866,867],{"class":66}," seam",[31,869,115],{"class":73},[31,871,872],{"class":66},"clockSeam",[31,874,176],{"class":73},[31,876,877],{"class":66},"clock",[31,879,880],{"class":73},".",[31,882,883],{"class":66},"AsFunction",[31,885,834],{"class":73},[31,887,888,891,893,896,898,901,903,906,909],{"class":33,"line":70},[31,889,890],{"class":66},"CHECK",[31,892,115],{"class":73},[31,894,895],{"class":66},"SecondsSince",[31,897,115],{"class":73},[31,899,900],{"class":145},"20",[31,902,137],{"class":73},[31,904,905],{"class":37},"==",[31,907,908],{"class":145}," 30",[31,910,269],{"class":73},[14,912,913,914,916],{},"The mock has to outlive what ",[17,915,883],{}," returns. A function pointer can only\ntake a function without state, so it reaches a mock of static lifetime through\none.",[427,918,920],{"id":919},"threads","Threads",[14,922,923,924,927],{},"Mocks count and order calls correctly when several threads call them. Reports\ngo through Catch's assertions, which are not safe across threads unless\n",[17,925,926],{},"CATCH_CONFIG_EXPERIMENTAL_THREAD_SAFE_ASSERTIONS"," is set, so a failure in a\nthread other than the test's needs that setting.",[14,929,930],{},"Outside of a test case, such as in the destructor of a global, there is nothing\nto fail: failures go to standard error.",[932,933,934],"style",{},"html pre.shiki code .snl16, html code.shiki .snl16{--shiki-default:#F97583}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .s95oV, html code.shiki .s95oV{--shiki-default:#E1E4E8}html pre.shiki code .s9osk, html code.shiki .s9osk{--shiki-default:#FFAB70}html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":27,"searchDepth":45,"depth":45,"links":936},[937,938,939,940,941],{"id":429,"depth":45,"text":430},{"id":527,"depth":45,"text":528},{"id":734,"depth":45,"text":735},{"id":803,"depth":45,"text":804},{"id":919,"depth":45,"text":920},"\u003CProgmasoft\u002FCatch3\u002FMock.hpp> replaces a dependency of the code under test\nwith a mock that checks how it is called. Mocks report through Catch's\nassertion path: an unexpected call, a call out of order and an unmet\nexpectation fail the test case that owns the mock, at the line that declared\nthe expectation, in every report the runner writes.","md",{},"\u002Fdocs\u002Fprogmasoft-mocking",{"title":5,"description":942},"progmasoft-mocking","docs\u002Fprogmasoft-mocking.md","docs\u002Fprogmasoft-mocking","S4B0Xu6j4o8dv-3bDz35yqsBpYrwaAp6L3mkf0JHsS4",1794961594465]