edit this page - page history - about editing

Using Capybara with Minitest for Integration Tests in Rails 4

Ruby on Rails / Capybara

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:

1. Add new bundles and run bundle install:

gem 'capybara'
gem 'launchy'

2. Create a new test/integration_test_helper.rb, just like the existing test_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

3. 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

4. Run your tests with rake test:integration (which will also be run as part of rake 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!')

Categories: Ruby | Ruby on Rails | Testing
edit this page - what links to here? - page history - top
Last edited by jevon jevon 31 months ago