Skip to content

Instantly share code, notes, and snippets.

@matthaliski
Last active December 19, 2015 09:49
Show Gist options
  • Save matthaliski/5935508 to your computer and use it in GitHub Desktop.
Save matthaliski/5935508 to your computer and use it in GitHub Desktop.
Rails Testing

Rails Testing

Below are some tools, workarounds, and gotchas to help speed things up when getting testing working.

Capybara


To get tests to run properly you'll need to add a line to spec_helper.rb file.

.
.
.
RSpec.configure do |config|
.
.
.
config.include Capybara::DSL
end

Testing in Sublime Text


After installing the RubyTest package you can issue the following commands to run tests directly from Sublime.

  • CMD-Shift-R: run a single test (if run on an it block) or group of tests (if run on a describe block)
  • CMD-Shift-E: run the last test(s)
  • CMD-Shift-T: run all the tests in current file

Speed things up with Spork


The Spork test server loads the environment once, and then maintains a pool of processes for running future tests.

Start Spork by going into your desired rails directory and issuing the spork command. It will then be up and running in that terminal window.

Configure RSpec to automatically use Spork by adding this to the .rspec file

--colour
--drb

Use Guard and Spork together


For fast real time testing you can combine Guard and Spork. First issue this command in terminal

guard init spork

Then you'll need to change the Guardfile

require 'active_support/inflector'

guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
               :rspec_env    => { 'RAILS_ENV' => 'test' } do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch('config/environments/test.rb')
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb') { :rspec }
  watch('test/test_helper.rb') { :test_unit }
  watch(%r{features/support/}) { :cucumber }
end

guard 'rspec', after_all_pass: false, cli: '--drb' do
  .
  .
  .
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment