Rambling Labs Blog Ramblings on software development

  • Unit testing the jQuery Rambling Slider - CoffeeScript, Jasmine and node.js

    As you may know, I have been working lately on the jQuery Rambling Slider. One of my personal milestones with this project is to write as many unit tests as possible, so I began my research.

    Honestly, I didn't know where to begin. I remember to have read once on twitter that someone was writing their tests with Jasmine and CoffeeScript, so that could be a good starting point (and it sounds really fun too!). I have worked with Jasmine before and it sure was a great experience. It's yet another productivity tool made by the great Pivotal Labs guys.

    After googling for a while, I stumbled into a post from someone that was writing tests in Jasmine and node.js. This is really awesome. Being able to run the Jasmine tests without firing up a browser, beautiful. In order to achieve this, you have to install jasmine-node through the node package manager like this:

    npm install -g jasmine-node

    Now, let's say you have a file src/array_extensions.coffee that looks like this:

    Array::contains = (value) ->
      length = @length
      for i in [0...length]
        return true if value is @[i]

    So you write this test in spec/array_extensions.spec.coffee, which shows how extremely beatiful is to write Jasmine tests on CoffeeScript:

    require '../src/array_extensions'
    describe 'Array Extensions', ->
      describe 'when verifying if an array contains an element', ->
        array = null
        beforeEach ->
          array = [1...5]
        it 'should return true for a contained element', ->
        it 'should return false for a non contained element', ->

    To run the tests just type:

    jasmine-node --coffee spec/

    Did you notice the sweet --coffee?!

    So that's where I began, writing tests for the simpler stuff first. I've just added a few tests, but I expect to bring that to 100% test coverage soon.


  • blog comments powered by Disqus