Created
November 6, 2011 05:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it "displays tasks", :js => true do | |
Task.create!(:name => "foo") | |
visit tasks_path | |
click_link 'some link' | |
page.should have_content("some text") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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
Thanks for this. Helped me out today.