Debian Codegraph @

inst/doc/mockr.html

raw
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13
14
15 <title>Mocking with mockr</title>
16
17 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
18 // be compatible with the behavior of Pandoc < 2.8).
19 document.addEventListener('DOMContentLoaded', function(e) {
20 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
21 var i, h, a;
22 for (i = 0; i < hs.length; i++) {
23 h = hs[i];
24 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
25 a = h.attributes;
26 while (a.length > 0) h.removeAttribute(a[0].name);
27 }
28 });
29 </script>
30
31 <style type="text/css">
32 code{white-space: pre-wrap;}
33 span.smallcaps{font-variant: small-caps;}
34 span.underline{text-decoration: underline;}
35 div.column{display: inline-block; vertical-align: top; width: 50%;}
36 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
37 ul.task-list{list-style: none;}
38 </style>
39
40
41
42 <style type="text/css">
43 code {
44 white-space: pre;
45 }
46 .sourceCode {
47 overflow: visible;
48 }
49 </style>
50 <style type="text/css" data-origin="pandoc">
51 html { -webkit-text-size-adjust: 100%; }
52 pre > code.sourceCode { white-space: pre; position: relative; }
53 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
54 pre > code.sourceCode > span:empty { height: 1.2em; }
55 .sourceCode { overflow: visible; }
56 code.sourceCode > span { color: inherit; text-decoration: inherit; }
57 div.sourceCode { margin: 1em 0; }
58 pre.sourceCode { margin: 0; }
59 @media screen {
60 div.sourceCode { overflow: auto; }
61 }
62 @media print {
63 pre > code.sourceCode { white-space: pre-wrap; }
64 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
65 }
66 pre.numberSource code
67 { counter-reset: source-line 0; }
68 pre.numberSource code > span
69 { position: relative; left: -4em; counter-increment: source-line; }
70 pre.numberSource code > span > a:first-child::before
71 { content: counter(source-line);
72 position: relative; left: -1em; text-align: right; vertical-align: baseline;
73 border: none; display: inline-block;
74 -webkit-touch-callout: none; -webkit-user-select: none;
75 -khtml-user-select: none; -moz-user-select: none;
76 -ms-user-select: none; user-select: none;
77 padding: 0 4px; width: 4em;
78 color: #aaaaaa;
79 }
80 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
81 div.sourceCode
82 { }
83 @media screen {
84 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
85 }
86 code span.al { color: #ff0000; font-weight: bold; }
87 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
88 code span.at { color: #7d9029; }
89 code span.bn { color: #40a070; }
90 code span.bu { color: #008000; }
91 code span.cf { color: #007020; font-weight: bold; }
92 code span.ch { color: #4070a0; }
93 code span.cn { color: #880000; }
94 code span.co { color: #60a0b0; font-style: italic; }
95 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
96 code span.do { color: #ba2121; font-style: italic; }
97 code span.dt { color: #902000; }
98 code span.dv { color: #40a070; }
99 code span.er { color: #ff0000; font-weight: bold; }
100 code span.ex { }
101 code span.fl { color: #40a070; }
102 code span.fu { color: #06287e; }
103 code span.im { color: #008000; font-weight: bold; }
104 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
105 code span.kw { color: #007020; font-weight: bold; }
106 code span.op { color: #666666; }
107 code span.ot { color: #007020; }
108 code span.pp { color: #bc7a00; }
109 code span.sc { color: #4070a0; }
110 code span.ss { color: #bb6688; }
111 code span.st { color: #4070a0; }
112 code span.va { color: #19177c; }
113 code span.vs { color: #4070a0; }
114 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
115 </style>
116 <script>
117 // apply pandoc div.sourceCode style to pre.sourceCode instead
118 (function() {
119 var sheets = document.styleSheets;
120 for (var i = 0; i < sheets.length; i++) {
121 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
122 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
123 var j = 0;
124 while (j < rules.length) {
125 var rule = rules[j];
126 // check if there is a div.sourceCode rule
127 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
128 j++;
129 continue;
130 }
131 var style = rule.style.cssText;
132 // check if color or background-color is set
133 if (rule.style.color === '' && rule.style.backgroundColor === '') {
134 j++;
135 continue;
136 }
137 // replace div.sourceCode by a pre.sourceCode rule
138 sheets[i].deleteRule(j);
139 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
140 }
141 }
142 })();
143 </script>
144
145
146
147
148 <style type="text/css">body {
149 background-color: #fff;
150 margin: 1em auto;
151 max-width: 700px;
152 overflow: visible;
153 padding-left: 2em;
154 padding-right: 2em;
155 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
156 font-size: 14px;
157 line-height: 1.35;
158 }
159 #TOC {
160 clear: both;
161 margin: 0 0 10px 10px;
162 padding: 4px;
163 width: 400px;
164 border: 1px solid #CCCCCC;
165 border-radius: 5px;
166 background-color: #f6f6f6;
167 font-size: 13px;
168 line-height: 1.3;
169 }
170 #TOC .toctitle {
171 font-weight: bold;
172 font-size: 15px;
173 margin-left: 5px;
174 }
175 #TOC ul {
176 padding-left: 40px;
177 margin-left: -1.5em;
178 margin-top: 5px;
179 margin-bottom: 5px;
180 }
181 #TOC ul ul {
182 margin-left: -2em;
183 }
184 #TOC li {
185 line-height: 16px;
186 }
187 table {
188 margin: 1em auto;
189 border-width: 1px;
190 border-color: #DDDDDD;
191 border-style: outset;
192 border-collapse: collapse;
193 }
194 table th {
195 border-width: 2px;
196 padding: 5px;
197 border-style: inset;
198 }
199 table td {
200 border-width: 1px;
201 border-style: inset;
202 line-height: 18px;
203 padding: 5px 5px;
204 }
205 table, table th, table td {
206 border-left-style: none;
207 border-right-style: none;
208 }
209 table thead, table tr.even {
210 background-color: #f7f7f7;
211 }
212 p {
213 margin: 0.5em 0;
214 }
215 blockquote {
216 background-color: #f6f6f6;
217 padding: 0.25em 0.75em;
218 }
219 hr {
220 border-style: solid;
221 border: none;
222 border-top: 1px solid #777;
223 margin: 28px 0;
224 }
225 dl {
226 margin-left: 0;
227 }
228 dl dd {
229 margin-bottom: 13px;
230 margin-left: 13px;
231 }
232 dl dt {
233 font-weight: bold;
234 }
235 ul {
236 margin-top: 0;
237 }
238 ul li {
239 list-style: circle outside;
240 }
241 ul ul {
242 margin-bottom: 0;
243 }
244 pre, code {
245 background-color: #f7f7f7;
246 border-radius: 3px;
247 color: #333;
248 white-space: pre-wrap;
249 }
250 pre {
251 border-radius: 3px;
252 margin: 5px 0px 10px 0px;
253 padding: 10px;
254 }
255 pre:not([class]) {
256 background-color: #f7f7f7;
257 }
258 code {
259 font-family: Consolas, Monaco, 'Courier New', monospace;
260 font-size: 85%;
261 }
262 p > code, li > code {
263 padding: 2px 0px;
264 }
265 div.figure {
266 text-align: center;
267 }
268 img {
269 background-color: #FFFFFF;
270 padding: 2px;
271 border: 1px solid #DDDDDD;
272 border-radius: 3px;
273 border: 1px solid #CCCCCC;
274 margin: 0 5px;
275 }
276 h1 {
277 margin-top: 0;
278 font-size: 35px;
279 line-height: 40px;
280 }
281 h2 {
282 border-bottom: 4px solid #f7f7f7;
283 padding-top: 10px;
284 padding-bottom: 2px;
285 font-size: 145%;
286 }
287 h3 {
288 border-bottom: 2px solid #f7f7f7;
289 padding-top: 10px;
290 font-size: 120%;
291 }
292 h4 {
293 border-bottom: 1px solid #f7f7f7;
294 margin-left: 8px;
295 font-size: 105%;
296 }
297 h5, h6 {
298 border-bottom: 1px solid #ccc;
299 font-size: 105%;
300 }
301 a {
302 color: #0033dd;
303 text-decoration: none;
304 }
305 a:hover {
306 color: #6666ff; }
307 a:visited {
308 color: #800080; }
309 a:visited:hover {
310 color: #BB00BB; }
311 a[href^="http:"] {
312 text-decoration: underline; }
313 a[href^="https:"] {
314 text-decoration: underline; }
315
316 code > span.kw { color: #555; font-weight: bold; }
317 code > span.dt { color: #902000; }
318 code > span.dv { color: #40a070; }
319 code > span.bn { color: #d14; }
320 code > span.fl { color: #d14; }
321 code > span.ch { color: #d14; }
322 code > span.st { color: #d14; }
323 code > span.co { color: #888888; font-style: italic; }
324 code > span.ot { color: #007020; }
325 code > span.al { color: #ff0000; font-weight: bold; }
326 code > span.fu { color: #900; font-weight: bold; }
327 code > span.er { color: #a61717; background-color: #e3d2d2; }
328 </style>
329
330
331
332
333 </head>
334
335 <body>
336
337
338
339
340 <h1 class="title toc-ignore">Mocking with mockr</h1>
341
342
343
344 <p>The mockr package helps testing code that relies on functions that
345 are slow, have unintended side effects or access resources that may not
346 be available when testing. It allows replacing such functions with
347 deterministic <a href="https://en.wikipedia.org/wiki/Mock_object"><em>mock
348 functions</em></a>. This article gives an overview and introduces a few
349 techniques.</p>
350 <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(mockr)</span></code></pre></div>
351 <div id="general-idea" class="section level2">
352 <h2>General idea</h2>
353 <p>Let’s assume a function <code>access_resource()</code> that accesses
354 some resource. This works in normal circumstances, but not during tests.
355 A function <code>work_with_resource()</code> works with that resource.
356 How can we test <code>work_with_resource()</code> without adding too
357 much logic to the implementation?</p>
358 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>access_resource <span class="ot">&lt;-</span> <span class="cf">function</span>() {</span>
359 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">&quot;Trying to access resource...&quot;</span>)</span>
360 <span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a> <span class="co"># For some reason we can&#39;t access the resource in our tests.</span></span>
361 <span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">&quot;Can&#39;t access resource now.&quot;</span>)</span>
362 <span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a>}</span>
363 <span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a></span>
364 <span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a>work_with_resource <span class="ot">&lt;-</span> <span class="cf">function</span>() {</span>
365 <span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a> resource <span class="ot">&lt;-</span> <span class="fu">access_resource</span>()</span>
366 <span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">&quot;Fetched resource: &quot;</span>, resource)</span>
367 <span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a> <span class="fu">invisible</span>(resource)</span>
368 <span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a>}</span></code></pre></div>
369 <p>In our example, calling the worker function gives an error:</p>
370 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">work_with_resource</span>()</span>
371 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#&gt; Trying to access resource...</span></span>
372 <span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#&gt; Error in access_resource(): Can&#39;t access resource now.</span></span></code></pre></div>
373 <p>We can use <code>local_mock()</code> to temporarily replace the
374 implementation of <code>access_resource()</code> with one that doesn’t
375 throw an error:</p>
376 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>access_resource_for_test <span class="ot">&lt;-</span> <span class="cf">function</span>() {</span>
377 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a> <span class="co"># We return a value that&#39;s good enough for testing</span></span>
378 <span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a> <span class="co"># and can be computed quickly:</span></span>
379 <span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a> <span class="dv">42</span></span>
380 <span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a>}</span>
381 <span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a></span>
382 <span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="fu">local</span>({</span>
383 <span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a> <span class="co"># Here, we override the function that raises the error</span></span>
384 <span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a> <span class="fu">local_mock</span>(<span class="at">access_resource =</span> access_resource_for_test)</span>
385 <span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a></span>
386 <span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a> <span class="fu">work_with_resource</span>()</span>
387 <span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a>})</span>
388 <span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#&gt; Fetched resource: 42</span></span></code></pre></div>
389 <p>The use of <code>local()</code> here is required for technical
390 reasons. This package is most useful in conjunction with testthat, the
391 remainder of this article will focus on that use case.</p>
392 </div>
393 <div id="create-demo-package" class="section level2">
394 <h2>Create demo package</h2>
395 <p>We create a package called {mocktest} for demonstration. For this
396 demo, the package is created in a temporary directory. A real project
397 will live somewhere in your home directory. The
398 <code>usethis::create_package()</code> function sets up a package
399 project ready for development. The output shows the details of the
400 package created.</p>
401 <div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>pkg <span class="ot">&lt;-</span> usethis<span class="sc">::</span><span class="fu">create_package</span>(<span class="fu">file.path</span>(<span class="fu">tempdir</span>(), <span class="st">&quot;mocktest&quot;</span>))</span>
402 <span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="co">#&gt; ✔ Creating</span></span>
403 <span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co">#&gt; &#39;/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpre1pOM/mocktest/&#39;.</span></span>
404 <span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="co">#&gt; ✔ Setting active project to</span></span>
405 <span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="co">#&gt; &quot;/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpre1pOM/mocktest&quot;.</span></span>
406 <span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="co">#&gt; ✔ Creating &#39;R/&#39;.</span></span>
407 <span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#&gt; ✔ Writing &#39;DESCRIPTION&#39;.</span></span>
408 <span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#&gt; Package: mocktest</span></span>
409 <span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#&gt; Title: What the Package Does (One Line, Title Case)</span></span>
410 <span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#&gt; Version: 0.0.0.9000</span></span>
411 <span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#&gt; Authors@R (parsed):</span></span>
412 <span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="co">#&gt; * First Last &lt;[email protected]&gt; [aut, cre]</span></span>
413 <span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="co">#&gt; Description: What the package does (one paragraph).</span></span>
414 <span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="co">#&gt; License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a</span></span>
415 <span id="cb5-15"><a href="#cb5-15" tabindex="-1"></a><span class="co">#&gt; license</span></span>
416 <span id="cb5-16"><a href="#cb5-16" tabindex="-1"></a><span class="co">#&gt; Encoding: UTF-8</span></span>
417 <span id="cb5-17"><a href="#cb5-17" tabindex="-1"></a><span class="co">#&gt; Roxygen: list(markdown = TRUE)</span></span>
418 <span id="cb5-18"><a href="#cb5-18" tabindex="-1"></a><span class="co">#&gt; RoxygenNote: 7.3.2.9000</span></span>
419 <span id="cb5-19"><a href="#cb5-19" tabindex="-1"></a><span class="co">#&gt; ✔ Writing &#39;NAMESPACE&#39;.</span></span>
420 <span id="cb5-20"><a href="#cb5-20" tabindex="-1"></a><span class="co">#&gt; ✔ Setting active project to &quot;&lt;no active project&gt;&quot;.</span></span></code></pre></div>
421 <p>In an interactive RStudio session, a new window opens. Users of other
422 environments would change the working directory manually. For this demo,
423 we manually set the active project.</p>
424 <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>usethis<span class="sc">::</span><span class="fu">proj_set</span>()</span>
425 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#&gt; ✔ Setting active project to</span></span>
426 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#&gt; &quot;/private/var/folders/dj/yhk9rkx97wn_ykqtnmk18xvc0000gn/T/Rtmpre1pOM/mocktest&quot;.</span></span></code></pre></div>
427 <p>The infrastructure files and directories that comprise a minimal R
428 package are created:</p>
429 <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>fs<span class="sc">::</span><span class="fu">dir_tree</span>()</span>
430 <span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co">#&gt; .</span></span>
431 <span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co">#&gt; ├── DESCRIPTION</span></span>
432 <span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="co">#&gt; ├── NAMESPACE</span></span>
433 <span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#&gt; └── R</span></span></code></pre></div>
434 </div>
435 <div id="import-function" class="section level2">
436 <h2>Import function</h2>
437 <p>We copy the functions from the previous example (under different
438 names) into the package. Normally we would use a text editor:</p>
439 <div class="sourceCode" id="cb8"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="fu">cat</span> <span class="op">&gt;</span> R/resource.R <span class="op">&lt;&lt;&quot;EOF&quot;</span></span>
440 <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="st">access_resource_pkg &lt;- function() {</span></span>
441 <span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="st"> message(&quot;Trying to access resource...&quot;)</span></span>
442 <span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="st"> # For some reason we can&#39;t access the resource in our tests.</span></span>
443 <span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="st"> stop(&quot;Can&#39;t access resource now.&quot;)</span></span>
444 <span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="st">}</span></span>
445 <span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a></span>
446 <span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="st">work_with_resource_pkg &lt;- function() {</span></span>
447 <span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="st"> resource &lt;- access_resource_pkg()</span></span>
448 <span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="st"> message(&quot;Fetched resource: &quot;, resource)</span></span>
449 <span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a><span class="st"> invisible(resource)</span></span>
450 <span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a><span class="st">}</span></span>
451 <span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a><span class="op">EOF</span></span></code></pre></div>
452 <p>Loading the package and calling the function gives the error we have
453 seen before:</p>
454 <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>pkgload<span class="sc">::</span><span class="fu">load_all</span>()</span>
455 <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a><span class="co">#&gt; ℹ Loading mocktest</span></span>
456 <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="fu">work_with_resource_pkg</span>()</span>
457 <span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="co">#&gt; Trying to access resource...</span></span>
458 <span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#&gt; Error in access_resource_pkg(): Can&#39;t access resource now.</span></span></code></pre></div>
459 </div>
460 <div id="adding-test-with-mock" class="section level2">
461 <h2>Adding test with mock</h2>
462 <p>We create a test that tests <code>work_with_resource_pkg()</code>,
463 mocking <code>access_resource_pkg()</code>. We need to prefix with the
464 package name, because testthat provides its own
465 <code>testthat::local_mock()</code> which is now deprecated.</p>
466 <div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>usethis<span class="sc">::</span><span class="fu">use_testthat</span>()</span>
467 <span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="co">#&gt; ✔ Adding testthat to &#39;Suggests&#39; field in DESCRIPTION.</span></span>
468 <span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="co">#&gt; ✔ Adding &quot;3&quot; to &#39;Config/testthat/edition&#39;.</span></span>
469 <span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co">#&gt; ✔ Creating &#39;tests/testthat/&#39;.</span></span>
470 <span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#&gt; ✔ Writing &#39;tests/testthat.R&#39;.</span></span>
471 <span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co">#&gt; ☐ Call `usethis::use_test()` to initialize a basic test file and open it for</span></span>
472 <span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#&gt; editing.</span></span></code></pre></div>
473 <div class="sourceCode" id="cb11"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="fu">cat</span> <span class="op">&gt;</span> tests/testthat/test-resource.R <span class="op">&lt;&lt;&quot;EOF&quot;</span></span>
474 <span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="st">test_that(&quot;Can work with resource&quot;, {</span></span>
475 <span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="st"> mockr::local_mock(access_resource_pkg = function() {</span></span>
476 <span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="st"> 42</span></span>
477 <span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="st"> })</span></span>
478 <span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a></span>
479 <span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="st"> expect_message(</span></span>
480 <span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="st"> expect_equal(work_with_resource_pkg(), 42)</span></span>
481 <span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="st"> )</span></span>
482 <span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="st">})</span></span>
483 <span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="op">EOF</span></span></code></pre></div>
484 <p>The test succeeds:</p>
485 <div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>testthat<span class="sc">::</span><span class="fu">test_local</span>(<span class="at">reporter =</span> <span class="st">&quot;location&quot;</span>)</span>
486 <span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="co">#&gt; </span></span>
487 <span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co">#&gt; Attaching package: &#39;testthat&#39;</span></span>
488 <span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a><span class="co">#&gt; The following objects are masked from &#39;package:mockr&#39;:</span></span>
489 <span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="co">#&gt; </span></span>
490 <span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="co">#&gt; local_mock, with_mock</span></span>
491 <span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#&gt; Start test: Can work with resource</span></span>
492 <span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a><span class="co">#&gt; &#39;test-resource.R:6:3&#39; [success]</span></span>
493 <span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a><span class="co">#&gt; &#39;test-resource.R:6:3&#39; [success]</span></span>
494 <span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a><span class="co">#&gt; End test: Can work with resource</span></span></code></pre></div>
495 </div>
496 <div id="run-individual-tests" class="section level2">
497 <h2>Run individual tests</h2>
498 <p>mockr is aware of testthat and will work even if executing the tests
499 in the current session. This is especially handy if you want to
500 troubleshoot single tests:</p>
501 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a><span class="fu">test_that</span>(<span class="st">&quot;Can work with resource&quot;</span>, {</span>
502 <span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> mockr<span class="sc">::</span><span class="fu">local_mock</span>(<span class="at">access_resource_pkg =</span> <span class="cf">function</span>() {</span>
503 <span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="dv">42</span></span>
504 <span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a> })</span>
505 <span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a></span>
506 <span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a> <span class="fu">expect_message</span>(</span>
507 <span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">work_with_resource_pkg</span>(), <span class="dv">42</span>)</span>
508 <span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a> )</span>
509 <span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a>})</span>
510 <span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a><span class="co">#&gt; Test passed 🥳</span></span></code></pre></div>
511 </div>
512 <div id="write-wrapper-functions" class="section level2">
513 <h2>Write wrapper functions</h2>
514 <p>mockr can only mock functions in the current package. To substitute
515 implementations of functions in other packages, create wrappers in your
516 package and use these wrappers exclusively.</p>
517 <p>The example below demonstrates a <code>d6()</code> function that is
518 used to get the value of a random die throw. Instead of using
519 <code>runif()</code> directly, this function uses
520 <code>my_runif()</code> which wraps <code>runif()</code>.</p>
521 <div class="sourceCode" id="cb14"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">cat</span> <span class="op">&gt;</span> R/runif.R <span class="op">&lt;&lt;&quot;EOF&quot;</span></span>
522 <span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="st">my_runif &lt;- function(...) {</span></span>
523 <span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="st"> runif(...)</span></span>
524 <span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="st">}</span></span>
525 <span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a></span>
526 <span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="st">d6 &lt;- function() {</span></span>
527 <span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="st"> trunc(my_runif(1, 0, 6)) + 1</span></span>
528 <span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a><span class="st">}</span></span>
529 <span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a><span class="op">EOF</span></span></code></pre></div>
530 <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>pkgload<span class="sc">::</span><span class="fu">load_all</span>()</span>
531 <span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="co">#&gt; ℹ Loading mocktest</span></span></code></pre></div>
532 <p>This allows testing the behavior of <code>d6()</code>:</p>
533 <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="fu">test_that</span>(<span class="st">&quot;d6() works correctly&quot;</span>, {</span>
534 <span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> seq <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="fl">0.32</span>, <span class="fl">5.4</span>, <span class="dv">5</span>, <span class="fl">2.99</span>)</span>
535 <span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> my_runif_mock <span class="ot">&lt;-</span> <span class="cf">function</span>(...) {</span>
536 <span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> <span class="fu">on.exit</span>(seq <span class="ot">&lt;&lt;-</span> seq[<span class="sc">-</span><span class="dv">1</span>])</span>
537 <span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a> seq[[<span class="dv">1</span>]]</span>
538 <span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a> }</span>
539 <span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a></span>
540 <span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a> mockr<span class="sc">::</span><span class="fu">local_mock</span>(<span class="at">my_runif =</span> my_runif_mock)</span>
541 <span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a></span>
542 <span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">d6</span>(), <span class="dv">1</span>)</span>
543 <span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">d6</span>(), <span class="dv">6</span>)</span>
544 <span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">d6</span>(), <span class="dv">6</span>)</span>
545 <span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a> <span class="fu">expect_equal</span>(<span class="fu">d6</span>(), <span class="dv">3</span>)</span>
546 <span id="cb16-14"><a href="#cb16-14" tabindex="-1"></a>})</span>
547 <span id="cb16-15"><a href="#cb16-15" tabindex="-1"></a><span class="co">#&gt; Test passed 😸</span></span></code></pre></div>
548 </div>
549 <div id="mock-s3-methods" class="section level2">
550 <h2>Mock S3 methods</h2>
551 <p>mockr cannot substitute implementations of S3 methods. To substitute
552 methods for a class <code>&quot;foo&quot;</code>, implement a subclass and add new
553 methods only for that subclass. The pillar package contains <a href="https://github.com/r-lib/pillar/blob/fd6376eca74e9748ed616c49f906529eaee68df9/tests/testthat/helper-unknown-rows.R">an
554 example</a> where a class with changed behavior for <code>dim()</code>
555 and <code>head()</code> for the sole purpose of testing.</p>
556 </div>
557
558
559
560 <!-- code folding -->
561
562
563 <!-- dynamically load mathjax for compatibility with self-contained -->
564 <script>
565 (function () {
566 var script = document.createElement("script");
567 script.type = "text/javascript";
568 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
569 document.getElementsByTagName("head")[0].appendChild(script);
570 })();
571 </script>
572
573 </body>
574 </html>