diff --git a/chapters/index.html b/chapters/index.html index dc0ad39..778f38e 100644 --- a/chapters/index.html +++ b/chapters/index.html @@ -15,6 +15,7 @@ - Networking - Design Patterns - Databases +- Testing ---
    diff --git a/chapters/testing/images/jasmine_failing_all.jpg b/chapters/testing/images/jasmine_failing_all.jpg new file mode 100644 index 0000000..29a601c Binary files /dev/null and b/chapters/testing/images/jasmine_failing_all.jpg differ diff --git a/chapters/testing/images/jasmine_failing_better.jpg b/chapters/testing/images/jasmine_failing_better.jpg new file mode 100644 index 0000000..0f212b0 Binary files /dev/null and b/chapters/testing/images/jasmine_failing_better.jpg differ diff --git a/chapters/testing/images/jasmine_passing.jpg b/chapters/testing/images/jasmine_passing.jpg new file mode 100644 index 0000000..8c3f2d7 Binary files /dev/null and b/chapters/testing/images/jasmine_passing.jpg differ diff --git a/chapters/testing/index.html b/chapters/testing/index.html new file mode 100644 index 0000000..a838b6f --- /dev/null +++ b/chapters/testing/index.html @@ -0,0 +1,18 @@ +--- +layout: chapter +title: Testing +chapter: Testing +--- + +{% capture url %}/chapters/{{ page.chapter | replace: ' ', '_' | downcase }}{% endcapture %} +{% capture indexurl %}{{ url }}/index.html{% endcapture %} + + diff --git a/chapters/testing/testing_with_jasmine.md b/chapters/testing/testing_with_jasmine.md new file mode 100644 index 0000000..4dfffb9 --- /dev/null +++ b/chapters/testing/testing_with_jasmine.md @@ -0,0 +1,200 @@ +--- +layout: recipe +title: Testing with Jasmine +chapter: Testing +--- +## Problem + +You are writing a simple calculator using CoffeeScript and you want to verify it functions as expected. You decide to use the Jasmine test framework. + +## Discussion + +When using the Jasmine test framework, you write tests in a specification (spec) file that describes the expected functionality of the code to be tested. + +For example, we expect our calculator will be able to add and subtract and will function correctly with both positive and negative numbers. Our spec is listed below. + +{% highlight coffeescript %} + +# calculatorSpec.coffee + +describe 'Calculator', -> + + it 'can add two positive numbers', -> + calculator = new Calculator() + result = calculator.add 2, 3 + expect(result).toBe 5 + + it 'can handle negative number addition', -> + calculator = new Calculator() + result = calculator.add -10, 5 + expect(result).toBe -5 + + it 'can subtract two positive numbers', -> + calculator = new Calculator() + result = calculator.subtract 10, 6 + expect(result).toBe 4 + + it 'can handle negative number subtraction', -> + calculator = new Calculator() + result = calculator.subtract 4, -6 + expect(result).toBe 10 + +{% endhighlight %} + + +### Configuring Jasmine + +Before you can run your tests, you must download and configure Jasmine. This involves: +1. downloading the latest Jasmine zip file; +2. creating a spec and a spec/jasmine folder in your project; +3. extracting the downloaded Jasmine files into the spec/jasmine folder; and +4. creating a test runner. + +### Create a Test Runner + +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. + +{% highlight html linenos %} + + + + + Jasmine Spec Runner + + + + + + + + + + + + + + + + + + + + +{% endhighlight %} + +This spec runner can be downloaded from this GitHub gist. + +To use the SpecRunner.html, simply reference your compiled JavaScript files and compiled tests after jasmine.js and its dependencies. + +In the above example, we include our yet-to-be-developed calculator.js file on line 14 and our compiled calculatorSpec.js file on line 17. + +## Running the Tests + +To run our tests, simply open SpecRunner.html in a web browser. In our example we see 4 failing specs with a total of 8 failures (below). + +All failing tests + +It appears our tests are failing because Jasmine cannot find the variable Calculator. That's because it has not been created yet. Let's do that now by creating a new file named js/calculator.coffee. + + +{% highlight coffeescript %} + +# calculator.coffee + +window.Calculator = class Calculator + +{% endhighlight %} + +Compile calculator.coffee and refresh the browser to re-run the test suite. + +Still failing, but better + +We now have 4 failures instead of our previous 8. That's a 50% improvement with only one line of code. + +## Getting the Tests to Pass + +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. + +All passing + + +## Refactoring the Tests + +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 initialization code into a routine that runs before each test. + +Luckily Jasmine has a beforeEach function just for this purpose. + +{% highlight coffeescript %} + +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 %} + +When we recompile our spec and refresh the browser we see the tests still all pass. + +All passing diff --git a/index.html b/index.html index 247136b..0b997af 100644 --- a/index.html +++ b/index.html @@ -15,6 +15,7 @@ - Networking - Design Patterns - Databases +- Testing ---

    Welcome