Skip to content

Instantly share code, notes, and snippets.

@kinopyo
Created November 6, 2011 05:58
Show Gist options
  • Save kinopyo/1342538 to your computer and use it in GitHub Desktop.
Save kinopyo/1342538 to your computer and use it in GitHub Desktop.
How to enable javascript in request(integration) test using rspec and capybara webkit. Also covered issue with database transaction.
# in your view erb file
<%= link_to_function "test js", '$(this).html("js works")' %>
# in test spec
it "supports js", :js => true do
visit tasks_path
click_link "test js"
page.should have_content("js works")
end
# webkit-dependency
# if you don't have homebrew installed
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
# if you don't have QT installed
brew install qt
# Gemfile, add capybara-webkit
group :test, :development do
...
gem "capybara-webkit"
end
# in Terminal
bundle install
# set default capybara driver to webkit.
# I put this into spec_helper.rb
Capybara.javascript_driver = :webkit
# In RSpec, use the :js => true flag
it "supports js", :js => true do
...
end
it "displays tasks", :js => true do
Task.create!(:name => "foo")
visit tasks_path
click_link 'some link'
page.should have_content("some text")
end
# in Gemfiile
group :test, :development do
...
gem "database_cleaner"
end
# in Terminal
bundle install
# in spec_helper.rb
RSpec.configure do |config|
# ...
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
@heironimus
Copy link

See http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ for how to use speedier transactional fixtures, when possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment