Skip to content

Completed Jasmine testing recipe. #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 6, 2012
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Completed Jasmine recipe
  • Loading branch information
OakRaven committed May 3, 2012
commit b62a29d0e690b30ffe319932601d7ba9fdb11639
Binary file added chapters/testing/images/jasmine_failing_all.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapters/testing/images/jasmine_passing.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions chapters/testing/testing_with_jasmine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
layout: recipe
title: With Jasmine
chapter: Testing
---
## Problem

You have some CoffeeScript and you want to verify that it is working correctly. You decide to the use
Jasmine test framework for your tests.

{% highlight coffeescript %}

# calculatorSpec.coffee

describe 'Calculator', ->

calculator = null

beforeEach ->
calculator = new Calculator()

it 'can add two positive numbers', ->
result = calculator.add 2, 3
expect(result).toBe 5

it 'can handle negative number addition', ->
result = calculator.add -10, 5
expect(result).toBe -5

it 'can subtract two positive numbers', ->
result = calculator.subtract 10, 6
expect(result).toBe 4

it 'can handle negative number subtraction', ->
result = calculator.subtract 4, -6
expect(result).toBe 10

{% endhighlight %}

## Discussion

This test describes our Calculator and tests that it can add and subtract positive and negative numbers.

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.
<img src="images/jasmine_failing_all.jpg" alt="All failing tests" />

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.

{% highlight coffeescript %}

# calculator.coffee

window.Calculator = class Calculator

{% endhighlight %}

When we re-run we see the following.

<img src="images/jasmine_failing_better.jpg" alt="Still failing, but better" />

We now have 4 failures instead of our previous 8. That's a 50% improvment with 1 line of code. Not bad.

Let's implement our methods and see if we can get these tests to pass.

{% highlight coffeescript %}

# calculator.coffee

window.Calculator = class Calculator
add: (a, b) ->
a + b

subtract: (a, b) ->
a - b

{% endhighlight %}

When we refresh we see they all pass.

<img src="images/jasmine_passing.jpg" alt="All passing" />
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
layout: recipe
title: Behavior Testing with Jasmine
title: With Mocha
chapter: Testing
---
## Problem

You have some CoffeeScript and you want to verify that it is working correctly.

You have some CoffeeScript and you want to verify that it is working correctly. You decide to the use
Mocha test framework for your tests.
13 changes: 13 additions & 0 deletions chapters/testing/testing_with_qunit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
layout: recipe
title: With QUint
chapter: Testing
---
## Problem

You have some CoffeeScript and you want to verify that it is working correctly. You decide to the use
QUnit test framework for your tests.

## Solution
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.

9 changes: 0 additions & 9 deletions chapters/testing/unit_testing_with_qunit.md

This file was deleted.