Skip to content

Commit 5559d9e

Browse files
committed
Merge pull request #73 from coffeescript-cookbook/nodeunit
Nodeunit Revamp
2 parents 0648bcf + 5d615da commit 5559d9e

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

Diff for: chapters/testing/testing_with_nodeunit.md

+226
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
---
2+
layout: recipe
3+
title: Testing with Nodeunit
4+
chapter: Testing
5+
---
6+
## Problem
7+
8+
You are writing a simple calculator using CoffeeScript and you want to verify it functions as expected. You decide to use the <a href="https://github.com/caolan/nodeunit" target="_blank">Nodeunit</a> test framework.
9+
10+
## Discussion
11+
12+
Nodeunit is a JavaScript implementation of the xUnit family of Unit Testing libraries, similar libraries are available for Java, Python, Ruby, Smalltalk etc.
13+
14+
When using xUnit family test frameworks, you write tests in a file that describes the expected functionality of the code to be tested.
15+
16+
For example, we expect our calculator will be able to add and subtract and will function correctly with both positive and negative numbers. Our test is listed below.
17+
18+
{% highlight coffeescript %}
19+
20+
# tests/calculator.test.coffee
21+
22+
Calculator = require '../calculator'
23+
24+
exports.CalculatorTest =
25+
26+
'test can add two positive numbers': (test) ->
27+
calculator = new Calculator
28+
result = calculator.add 2, 3
29+
test.equal(result, 5)
30+
test.done()
31+
32+
'test can handle negative number addition': (test) ->
33+
calculator = new Calculator
34+
result = calculator.add -10, 5
35+
test.equal(result, -5)
36+
test.done()
37+
38+
'test can subtract two positive numbers': (test) ->
39+
calculator = new Calculator
40+
result = calculator.subtract 10, 6
41+
test.equal(result, 4)
42+
test.done()
43+
44+
'test can handle negative number subtraction': (test) ->
45+
calculator = new Calculator
46+
result = calculator.subtract 4, -6
47+
test.equal(result, 10)
48+
test.done()
49+
50+
{% endhighlight %}
51+
52+
### Installing Nodeunit
53+
54+
Before you can run your tests, you must install Nodeunit:
55+
56+
First of all create a `package.json` file
57+
58+
{% highlight javascript %}
59+
{
60+
"name": "calculator",
61+
"version": "0.0.1",
62+
"scripts": {
63+
"test": "./node_modules/.bin/nodeunit test"
64+
},
65+
"dependencies": {
66+
"coffee-script": "~1.4.0",
67+
"nodeunit": "~0.7.4"
68+
}
69+
}
70+
{% endhighlight %}
71+
72+
Next from a terminal run.
73+
74+
{% highlight bash %}
75+
$ npm install
76+
{% endhighlight %}
77+
78+
## <span style="color: red;">Running the Tests</span>
79+
80+
It's easy to run the tests from the command-line:
81+
82+
{% highlight bash %}
83+
$ npm test
84+
{% endhighlight %}
85+
86+
The test runner should fail, because we have no calculator.coffee
87+
88+
suki@Yuzuki:nodeunit_testing (master)$ npm test
89+
npm WARN package.json [email protected] No README.md file found!
90+
91+
> [email protected] test /Users/suki/tmp/nodeunit_testing
92+
> ./node_modules/.bin/nodeunit test
93+
94+
95+
/Users/suki/tmp/nodeunit_testing/node_modules/nodeunit/lib/nodeunit.js:72
96+
if (err) throw err;
97+
^
98+
Error: ENOENT, stat '/Users/suki/tmp/nodeunit_testing/test'
99+
npm ERR! Test failed. See above for more details.
100+
npm ERR! not ok code 0
101+
102+
Let's create a simple file
103+
104+
{% highlight coffeescript %}
105+
106+
# calculator.coffee
107+
108+
class Calculator
109+
110+
module.exports = Calculator
111+
{% endhighlight %}
112+
113+
And re-run the test suite.
114+
115+
suki@Yuzuki:nodeunit_testing (master)$ npm test
116+
npm WARN package.json [email protected] No README.md file found!
117+
118+
> [email protected] test /Users/suki/tmp/nodeunit_testing
119+
> ./node_modules/.bin/nodeunit test
120+
121+
122+
calculator.test
123+
✖ CalculatorTest - test can add two positive numbers
124+
125+
TypeError: Object #<Calculator> has no method 'add'
126+
...
127+
128+
✖ CalculatorTest - test can handle negative number addition
129+
130+
TypeError: Object #<Calculator> has no method 'add'
131+
...
132+
133+
✖ CalculatorTest - test can subtract two positive numbers
134+
135+
TypeError: Object #<Calculator> has no method 'subtract'
136+
...
137+
138+
✖ CalculatorTest - test can handle negative number subtraction
139+
140+
TypeError: Object #<Calculator> has no method 'subtract'
141+
...
142+
143+
144+
FAILURES: 4/4 assertions failed (31ms)
145+
npm ERR! Test failed. See above for more details.
146+
npm ERR! not ok code 0
147+
148+
149+
## <span style="color: green;">Getting the Tests to Pass</span>
150+
151+
Let's implement our methods and see if we can get these tests to pass.
152+
153+
{% highlight coffeescript %}
154+
155+
# calculator.coffee
156+
157+
class Calculator
158+
159+
add: (a, b) ->
160+
a + b
161+
162+
subtract: (a, b) ->
163+
a - b
164+
165+
module.exports = Calculator
166+
{% endhighlight %}
167+
168+
When we rerun the tests we see they're all passing:
169+
170+
suki@Yuzuki:nodeunit_testing (master)$ npm test
171+
npm WARN package.json [email protected] No README.md file found!
172+
173+
> [email protected] test /Users/suki/tmp/nodeunit_testing
174+
> ./node_modules/.bin/nodeunit test
175+
176+
177+
calculator.test
178+
✔ CalculatorTest - test can add two positive numbers
179+
✔ CalculatorTest - test can handle negative number addition
180+
✔ CalculatorTest - test can subtract two positive numbers
181+
✔ CalculatorTest - test can handle negative number subtraction
182+
183+
OK: 4 assertions (27ms)
184+
185+
186+
## <span style="color: green;">Refactoring the Tests</span>
187+
188+
Now that our tests pass, we should look to see if our code or our test(s) can be refactored.
189+
190+
In our test file, each test creates its own calculator instance. This can make our tests quite repetitive especially for larger test suites. Ideally, we should consider moving that initialization code into a routine that runs before each test.
191+
192+
In common with other xUnit libraries, Nodeunit provides a setUp (and tearDown) function which will be called before each test.
193+
194+
{% highlight coffeescript %}
195+
196+
Calculator = require '../calculator'
197+
198+
exports.CalculatorTest =
199+
200+
setUp: (callback) ->
201+
@calculator = new Calculator
202+
callback()
203+
204+
'test can add two positive numbers': (test) ->
205+
result = @calculator.add 2, 3
206+
test.equal(result, 5)
207+
test.done()
208+
209+
'test can handle negative number addition': (test) ->
210+
result = @calculator.add -10, 5
211+
test.equal(result, -5)
212+
test.done()
213+
214+
'test can subtract two positive numbers': (test) ->
215+
result = @calculator.subtract 10, 6
216+
test.equal(result, 4)
217+
test.done()
218+
219+
'test can handle negative number subtraction': (test) ->
220+
result = @calculator.subtract 4, -6
221+
test.equal(result, 10)
222+
test.done()
223+
224+
{% endhighlight %}
225+
226+
We can rerun the tests and everything should continue to pass.

0 commit comments

Comments
 (0)