|
| 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" /> |
0 commit comments