Skip to content

Commit b62a29d

Browse files
committed
Completed Jasmine recipe
1 parent 86a90af commit b62a29d

File tree

7 files changed

+96
-12
lines changed

7 files changed

+96
-12
lines changed
238 KB
Loading
169 KB
Loading
25.8 KB
Loading
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
layout: recipe
3+
title: With Jasmine
4+
chapter: Testing
5+
---
6+
## Problem
7+
8+
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.
10+
11+
{% highlight coffeescript %}
12+
13+
# calculatorSpec.coffee
14+
15+
describe 'Calculator', ->
16+
17+
calculator = null
18+
19+
beforeEach ->
20+
calculator = new Calculator()
21+
22+
it 'can add two positive numbers', ->
23+
result = calculator.add 2, 3
24+
expect(result).toBe 5
25+
26+
it 'can handle negative number addition', ->
27+
result = calculator.add -10, 5
28+
expect(result).toBe -5
29+
30+
it 'can subtract two positive numbers', ->
31+
result = calculator.subtract 10, 6
32+
expect(result).toBe 4
33+
34+
it 'can handle negative number subtraction', ->
35+
result = calculator.subtract 4, -6
36+
expect(result).toBe 10
37+
38+
{% endhighlight %}
39+
40+
## Discussion
41+
42+
This test describes our Calculator and tests that it can add and subtract positive and negative numbers.
43+
44+
To test our specification (spec), we need to set up our test framework. Refer to the <a href="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.
45+
<img src="images/jasmine_failing_all.jpg" alt="All failing tests" />
46+
47+
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.
48+
49+
{% highlight coffeescript %}
50+
51+
# calculator.coffee
52+
53+
window.Calculator = class Calculator
54+
55+
{% endhighlight %}
56+
57+
When we re-run we see the following.
58+
59+
<img src="images/jasmine_failing_better.jpg" alt="Still failing, but better" />
60+
61+
We now have 4 failures instead of our previous 8. That's a 50% improvment with 1 line of code. Not bad.
62+
63+
Let's implement our methods and see if we can get these tests to pass.
64+
65+
{% highlight coffeescript %}
66+
67+
# calculator.coffee
68+
69+
window.Calculator = class Calculator
70+
add: (a, b) ->
71+
a + b
72+
73+
subtract: (a, b) ->
74+
a - b
75+
76+
{% endhighlight %}
77+
78+
When we refresh we see they all pass.
79+
80+
<img src="images/jasmine_passing.jpg" alt="All passing" />
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
layout: recipe
3-
title: Behavior Testing with Jasmine
3+
title: With Mocha
44
chapter: Testing
55
---
66
## Problem
77

8-
You have some CoffeeScript and you want to verify that it is working correctly.
9-
8+
You have some CoffeeScript and you want to verify that it is working correctly. You decide to the use
9+
Mocha test framework for your tests.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
layout: recipe
3+
title: With QUint
4+
chapter: Testing
5+
---
6+
## Problem
7+
8+
You have some CoffeeScript and you want to verify that it is working correctly. You decide to the use
9+
QUnit test framework for your tests.
10+
11+
## Solution
12+
In this example, we are creating a simple calculator class that can add and subtract two numbers. We will begin by writing our tests in CoffeeScript.
13+

chapters/testing/unit_testing_with_qunit.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)