Search code examples
ruby-on-railstestingangularjsasset-pipelinejasmine

How do I set up a Rails Angular project to test JS?


I am trying to set up my Rails Angular project to provide JS tests. I have tried almost everything that I have found on Google:

but I failed with all of them. I'm looking for a way to run unit and e2e tests in the most painless way (it can be in Guard or Karma, I don't care, but it must run it automatically in the background).

Does anyone have some nice article with a nice example of how to achieve this? In my research I have found this, but IMHO it is an example of how to NOT do this.

My actual Gemfile:

source 'https://rubygems.org'

# Use Ruby 1.9.3 instead default Heroku's 1.9.2
# for development I suggest https://gist.github.com/1688857
ruby '1.9.3'

gem 'rails', '3.2.12'

# Use PostgreSQL, which is quite awesome, fast and easy
gem 'pg'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', '~> 3.2.3'
  gem 'bootstrap-sass', '~> 2.3.1'

  # I heard that you like Compass
  gem 'compass'

  # Angular.js
  gem 'angularjs-rails'
  gem 'angularjs-rails-resource'
  gem 'angular-ui-rails'

  # Assets should be minified before production
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# Serve static pages like a boss
gem 'high_voltage'

# Some user management will be nice
gem 'devise' # User management
# gem 'cancan' # And they privileges

# To use Jbuilder templates for JSON
gem 'jbuilder'

# Be fast and deadly as Puma
#gem 'puma'

# We need also some administration panel, don't we?
gem 'rails_admin'

# Some helpers
gem 'andand'

group :development do
  # IRb is ugly. Use Pry for the God's sake
  gem 'pry-rails'

  # Deploy with Capistrano
  # gem 'capistrano'
  # or Vlad the Deployer
  # gem 'vlad'

  # Why bother yourself with rerunning tests? Use Guard
  gem 'guard'
  gem 'guard-rspec'
  gem 'guard-spork'
  gem 'guard-livereload'
  gem 'guard-jasmine'
  gem 'rb-fsevent', require: false
  gem 'rb-inotify', require: false

  # Who like ugly error pages? We don't.
  gem 'better_errors'
  gem 'binding_of_caller'

  # Prettier documentation
  gem 'yard'
end

group :development, :test do
  # Use RSpec for testing
  gem 'rspec-rails', '~> 2.12.0'

  # Test JS using Jasmine
  gem 'jasmine'
  gem 'jasmine-headless-webkit'

  # Some DB table generator
  gem 'factory_girl_rails', '~> 4.1.0'

  # And fake data generator
  gem 'ffaker'
end

group :test do
  # Some Gherkins will be also good (to vodka of course)
  gem 'turnip', '~> 1.1.0'

  # Aww, an of course some web browser will be also apprised
  gem 'capybara', '~> 2.0.1'

  # Clean DB after tests
  gem 'database_cleaner'

  # Some nice matchers
  gem 'shoulda-matchers'

  # Extend your mocks
  gem 'bourne', '~> 1.2.1'

  # Coverage reports will be nice
  gem 'simplecov', require: false
end

PS: It would be nice if I can create coverage reports in a simple way.


Solution

  • You can use teaspoon it runs your favorite test suite (default is jasmine) and they have a wiki to integrate with angular. Teaspoon integrates with the asset pipeline, so you can throw all the angular gems in there and require them in the teaspoon generated spec_helper.js.

    The other cool thing is: they have a guard plugin available as well.