TODO: Check out http://sinonjs.org/ to stub AJAX requests or just stubbing with Jasmine to test expectations
- Source: https://github.com/searls/jasmine-rails
- Install PhantomJS on Mac:
brew install phantomjs - Add gem
gem 'jasmine-rails'togroup :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
- 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- From command line,
RAILS_ENV=test bundle exec rake spec:javascriptorrake spec:javascript - From server, go to
http://localhost:3000/specsorhttp://project.dev/specsfor POW
Refer to http://jasmine.github.io/2.0/introduction.html
//= 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)