Skip to content

Instantly share code, notes, and snippets.

@stevenyap
Created February 27, 2014 03:57
Show Gist options
  • Select an option

  • Save stevenyap/836fa5ffcb26ae4ad169 to your computer and use it in GitHub Desktop.

Select an option

Save stevenyap/836fa5ffcb26ae4ad169 to your computer and use it in GitHub Desktop.
Jasmine - JS Testing Framework

TODO: Check out http://sinonjs.org/ to stub AJAX requests or just stubbing with Jasmine to test expectations

Setup for rails

  • Source: https://github.com/searls/jasmine-rails
  • Install PhantomJS on Mac: brew install phantomjs
  • Add gem gem 'jasmine-rails' to group :test, :development
  • bundle install
  • Run setup: rails generate jasmine_rails:install

Note: You don't have to install the jasmine gem from Pivotal. Reference (FYI only): https://github.com/pivotal/jasmine-gem

Setup tests

  • Assuming you have a file at /app/assets/javascripts/users/geolocation.js.coffee
  • Add your tests to /spec/javascripts/users/geolocation_spec.js.coffee
  • Sample JS test as below:
//= require users/geolocation

describe "Foo", ->
  it "does something", ->
    expect(1 + 1).toBe 2
    return
  return

Running tests

  • From command line, RAILS_ENV=test bundle exec rake spec:javascript or rake spec:javascript
  • From server, go to http://localhost:3000/specs or http://project.dev/specs for POW

How to use Jasmine

Refer to http://jasmine.github.io/2.0/introduction.html

Sample Jasmine Test Case

//= require users/geolocation

describe 'geolocation', ->
  user_id = 1

  it 'gets geolocation data from browser', ->
    spyOn(navigator.geolocation, 'getCurrentPosition')
    geolocation.initiate(user_id)
    expect(navigator.geolocation.getCurrentPosition).toHaveBeenCalled()

  it 'sets user_id', ->
    geolocation.initiate(user_id)
    expect(geolocation.user_id).toBe(user_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment