Using Capybara with Minitest for Integration Tests in Rails 4
Not as obvious or straightforward as it should be, since most Capybara code is based on RSpec, but Rails 4 comes with Minitest by default. Based on this Rails 3 guide:
- Add new bundles and run
bundle install
:
gem 'capybara'
gem 'launchy'
- Create a new
test/integration_test_helper.rb
, just like the existingtest_helper.rb
there, to import all of the Capybara DSL methods:
require "test_helper"
require "capybara/rails"
module ActionDispatch
class IntegrationTest
include Capybara::DSL
end
end
- Create a test under
test/integration/create_player_test.rb
(this extends the same normal integration tests superclass ActionDispatch::IntegrationTest):
require "integration_test_helper"
class CreatePlayerTest < ActionDispatch::IntegrationTest
setup do
@user = User.new(name: "Test user", provider: "testing")
@user.save()
end
teardown do
@user.destroy
end
def do_login!
# login through test method
visit "/sessions/test?id=#{@user.id}"
assert_equal "/player/new", current_path
end
test "create a new player" do
do_login!
visit "/player/new"
assert page.has_field?("Name", with: @user.name)
click_button "Start playing"
assert page.has_content?("You are currently at Home")
end
end
- Run your tests with
rake test:integration
(which will also be run as part ofrake test
).
Assertions such as expect(current_path).to == "/player/new"
and current_path.should == "/player/new"
are RSpec idioms, and they won’t work with Minitest assertions.
Optionally you may wish to add the capybara_minitest_spec
gem so you can have more informational failure messages:
page.must_have_content('Important!')