You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You have some CoffeeScript and you want to verify that it is working correctly. You decide to the use
9
-
Jasmine test framework for your tests.
8
+
You are writing a new calculator library using CoffeeScript code and you want to verify it functions as expected. You decide to use the <ahref="http://pivotal.github.com/jasmine/"target="_blank">Jasmine</a> test framework.
9
+
10
+
## Discussion
11
+
12
+
When using the Jasmine test framework, you write tests in a spec that describe the expected functionality of the code to be tested.
13
+
14
+
For example, we expect our calculator will be able to add and subtract both positive and negative numbers correctly. Our spec is listed below.
10
15
11
16
{% highlight coffeescript %}
12
17
13
18
# calculatorSpec.coffee
14
19
15
20
describe 'Calculator', ->
16
21
17
-
calculator = null
18
-
19
-
beforeEach ->
20
-
calculator = new Calculator()
21
-
22
22
it 'can add two positive numbers', ->
23
+
calculator = new Calculator()
23
24
result = calculator.add 2, 3
24
25
expect(result).toBe 5
25
26
26
27
it 'can handle negative number addition', ->
28
+
calculator = new Calculator()
27
29
result = calculator.add -10, 5
28
30
expect(result).toBe -5
29
31
30
32
it 'can subtract two positive numbers', ->
33
+
calculator = new Calculator()
31
34
result = calculator.subtract 10, 6
32
35
expect(result).toBe 4
33
36
34
37
it 'can handle negative number subtraction', ->
38
+
calculator = new Calculator()
35
39
result = calculator.subtract 4, -6
36
40
expect(result).toBe 10
37
41
38
42
{% endhighlight %}
39
43
40
-
## Discussion
41
44
42
-
This test describes our Calculator and tests that it can add and subtract positive and negative numbers.
45
+
### Configuring Jasmine
46
+
47
+
Before you can run your tests, you must download and configure Jasmine. This involves:
48
+
1. Download the latest <ahref="http://pivotal.github.com/jasmine/download.html"target="_blank">Jasmine</a> zip file
49
+
2. Create a spec and a spec/jasmine folder in your project
50
+
3. Extract the Jasmine files into the spec/jasmine folder
51
+
4. Create a test runner
52
+
53
+
### Create a Test Runner
54
+
55
+
Jasmine can run your tests within a web browser by using a spec runner HTML file. The spec runner is a simple HTML page that links the necessary JavaScript and CSS files for both Jasmine and your code. A sample is below.
56
+
57
+
{% highlight html linenos %}
58
+
59
+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
var trivialReporter =newjasmine.TrivialReporter();
86
+
87
+
jasmineEnv.addReporter(trivialReporter);
88
+
89
+
jasmineEnv.specFilter=function(spec) {
90
+
returntrivialReporter.specFilter(spec);
91
+
};
92
+
93
+
var currentWindowOnload =window.onload;
94
+
95
+
window.onload=function() {
96
+
if (currentWindowOnload) {
97
+
currentWindowOnload();
98
+
}
99
+
execJasmine();
100
+
};
101
+
102
+
functionexecJasmine() {
103
+
jasmineEnv.execute();
104
+
}
105
+
106
+
})();
107
+
</script>
108
+
</body>
109
+
</html>
110
+
111
+
{% endhighlight %}
112
+
113
+
This spec runner can be downloaded from this GitHub <ahref="https://gist.github.com/2623232"target="_blank">gist</a>.
114
+
115
+
To use the SpecRunner.html, simply reference your compiled JavaScript files and compiled tests after jasmine.js and its dependencies.
116
+
117
+
In the example, you can see we include our yet-to-be-developed calculator.js file (line 14) and the our compiled calculatorSpec.js file (line 17).
118
+
119
+
## <spanstyle="color: red;">Running the Tests</span>
120
+
121
+
To run our tests, simply open SpecRunner.html in a web browser. In this example we see 4 failing specs with a total of 8 failures (below).
43
122
44
-
To test our specification (spec), we need to set up our test framework. Refer to the <ahref="http://pivotal.github.com/jasmine/"target="_blank">Jasmine</a> website to download the framework. It's super easy. In the following example, we have our SpecRunner.html set up referencing the Jasmine JavaScript llibrary and css files. Our tests are also referenced. You can see the result of running out tests below.
The tests are all failing, complaining that Calculator does not exist. Of course it doesn't, we haven't created it yet. Let's do that now.
125
+
It appears our tests are failing because Jasmine can not find the variable Calculator. That's because it has not been created yet. Let's do that now in a new file named js/calculator.coffee.
126
+
48
127
49
128
{% highlight coffeescript %}
50
129
@@ -60,6 +139,8 @@ When we re-run we see the following.
60
139
61
140
We now have 4 failures instead of our previous 8. That's a 50% improvment with 1 line of code. Not bad.
62
141
142
+
## <spanstyle="color: green;">Getting the Tests to Pass</span>
143
+
63
144
Let's implement our methods and see if we can get these tests to pass.
64
145
65
146
{% highlight coffeescript %}
@@ -77,4 +158,39 @@ window.Calculator = class Calculator
## <spanstyle="color: green;">Refactoring the Tests</span>
165
+
166
+
Now that our tests pass, we should look to see if our code or our test(s) can be refactored. In our spec 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 initializaton code into a routine that runs before each test.
167
+
168
+
{% highlight coffeescript %}
169
+
170
+
describe 'Calculator', ->
171
+
calculator = null
172
+
173
+
beforeEach ->
174
+
calculator = new Calculator()
175
+
176
+
it 'can add two positive numbers', ->
177
+
result = calculator.add 2, 3
178
+
expect(result).toBe 5
179
+
180
+
it 'can handle negative number addition', ->
181
+
result = calculator.add -10, 5
182
+
expect(result).toBe -5
183
+
184
+
it 'can subtract two positive numbers', ->
185
+
result = calculator.subtract 10, 6
186
+
expect(result).toBe 4
187
+
188
+
it 'can handle negative number subtraction', ->
189
+
result = calculator.subtract 4, -6
190
+
expect(result).toBe 10
191
+
192
+
{% endhighlight %}
193
+
194
+
When we recompile our spec and refresh the browser we see the tests still all pass.
0 commit comments