-
Notifications
You must be signed in to change notification settings - Fork 2
/
interactions.html
111 lines (94 loc) · 5.7 KB
/
interactions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Expectations by jaycfields</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
<script src="javascripts/scale.fix.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<header>
<h1 class="header">Expectations</h1>
<p class="header">a minimalist's unit testing framework</p>
<ul>
<li><a class="buttons" href="index.html">Home</a></li>
<li><a class="buttons" href="installing.html">Installing</a></li>
<li><a class="buttons" href="introduction.html">Introduction</a></li>
<li><a class="buttons" href="advanced.html">Advanced</a></li>
<li><a class="buttons" href="collections.html">Collections</a></li>
<li><a class="buttons" href="templating.html">Expect More</a></li>
<li><a class="buttons" href="odds-ends.html">Odds & Ends</a></li>
<li><a class="buttons" href="in-context.html">Around Each</a></li>
<li><a class="buttons" href="before-run-hook.html">Around Suite</a></li>
<li><a class="buttons" href="state-warnings.html">State Warnings</a></li>
<li><a class="buttons" href="redef-state.html">Redef State</a></li>
<li><a class="buttons" href="interactions.html">Side Effects</a></li>
<li><a class="buttons" href="freeze-time.html">Freeze Time</a></li>
<li><a class="buttons" href="env-tweaks.html">ENV Tweaks</a></li>
<li><a class="buttons" href="emacs-tweaks.html">emacs Tweaks</a></li>
<li><a class="buttons" href="clojure-test.html">clojure.test</a></li>
<li><a class="buttons github" href="https://github.com/clojure-expectations/expectations">View On GitHub</a></li>
</ul>
<p class="header">This project is maintained by <a class="header name" href="https://github.com/seancorfield">seancorfield</a> and <a class="header name" href="https://github.com/jaycfields">jaycfields</a></p>
</header>
<section>
<h1>
<a name="expectations" class="anchor" href="#expectations"><span class="octicon octicon-link"></span></a>expectations</h1>
<blockquote>
<p>adding signal, removing noise</p>
</blockquote>
<h2>
<a name="interactions" class="anchor" href="#interactions"><span class="octicon octicon-link"></span></a>Testing Side Effects</h2>
<h4>side-effects</h4>
expectations 2.0 introduces the 'side-effects macro, which allows you to capture arguments to functions you specify. Previous to version 2.0 behavior was often tested using the 'interaction macro. expectations 2.0 removes the 'interaction macro and embraces the idea of verifying interactions at the data level, using the same type of comparison that is used for all other data.
<br><br>
Examples:
<br><br><pre>(expect [["/tmp/hello-world" "some data" :append true]
["/tmp/hello-world" "some data" :append true]]
(side-effects [spit]
(spit "/tmp/hello-world" "some data" :append true)
(spit "/tmp/hello-world" "some data" :append true)))</pre>
In the above example, you specify that spit is a side effect fn, and the 'side-effects macro will return a list of all calls made, with the arguments used in the call. The above example uses simple equality for verification.
<br><br><pre>(expect empty?
(side-effects [spit] "spit never called"))</pre>
The above example demonstrates how you can use non-equality to verify the data returned.
<br><br><pre>(expect ["/tmp/hello-world" "some data" :append true]
(in (side-effects [spit]
(spit "some other stuff" "xy")
(spit "/tmp/hello-world" "some data" :append true))))</pre>
Immediately above is an example of combining 'side-effects with 'in for a more concise test. Here we're testing that the expected data will exist somewhere within the list returned by 'side-effects
<h4>combining side-effects and more-of</h4>
It's fairly common to expect some behavior where you know the exact values for some of the args,
and you have something general in mind (or you'd like to ignore) the additional args.
By combining 'side-effects and 'more-of you can easily destructure a call into
it's args and verify whatever you care to..
<br><br>
note: if you haven't read <a href="templating.html">Expect More</a>, you'll probably want to do that first.
<br><br><pre>(expect (more-of [path data action {:keys [a c]}]
String path
#"some da" data
keyword? action
:b a
:d c)
(in (side-effects [spit]
(spit "/tmp/hello-world" "some data" :append {:a :b :c :d :e :f}))))</pre>
The above test is a bit much to swallow at first glance; however, it's actually very straightforward
once you've gotten used to the 'more-of syntax. In the above example the 'spit fn is called with
the args "/tmp/hello-world", "some data" :append {:a :b :c :d :e :f}. Using 'more-of,
we destructure those args, and expect them individually. The path arg is expected to be of type String.
The data arg is expected to be a string that matches the regex #"some da". The action is expected to be a 'keyword?. Finally, the options map is destructured to it's :a and :c values, and equality expected.
</section>
<footer>
<p><small>Hosted on <a href="https://pages.github.com">GitHub Pages</a> using the Dinky theme</small></p>
</footer>
</div>
<!--[if !IE]><script>fixScale(document);</script><![endif]-->
</body>
</html>