ponkiti's blog

主に自分用、イベント参加メモや備忘録として利用

ArgumentError: rack-test requires a rack application, but none was given

2014/12/19追記:

前回第3章を進めていた時は spec_helper.rb が存在しており、今回進めた時は spec_helper.rb がなかったということで、単純にrails generate rspec:installを実行し忘れていたのが原因。

$ rails generate rspec:install
      create  .rspec
       exist  spec
      create  spec/spec_helper.rb

『パーフェクトRuby on Rails』(p244)を読むと下記のように書いてあった。

spec_helper.rb は 、RSpec を利用したテストを行う際に利用する設定ファイル

rails generate rspec:installして生成された spec_helper.rb の内容

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

app/views/static_pages/home.html.erb を修正せずにテストコードを実行すると、Railsチュートリアルの実行結果と同じ内容になる。

$ bundle exec rspec spec/requests/static_pages_spec.rb
F

Failures:

  1) Static pages Home page should have the content 'Sample App'
     Failure/Error: expect(page).to have_content('Sample App')
       expected #has_content?("Sample App") to return true, got false
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

Finished in 0.03245 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:6 # Static pages Home page should have the content 'Sample App'

Randomized with seed 34550




Railsチュートリアル第3章のリスト3.11「Homeページ用コード」を追加後、テストコードを実行しようとしたが、図3.4のようにテストが成功しなかった。

修正前

各ファイルの設定

Gemfile

(省略)

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
end

(省略)

spec/requests/static_pages_spec.rb

require 'spec_helper'

describe "Static pages" do

  describe "Home page" do
    it "should have the content 'Sample App'" do
      visit '/static_pages/home'
      expect(page).to have_content('Sample App')
    end
  end

end

spec/spec_helper.rb

require 'capybara/rspec'

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

テストコードの実行

app/views/static_pages/home.html.erb は修正済みで Failures:1 は出ないはずだが、テストが成功しない。
※そして何故かRailsチュートリアルの図3.3とメッセージが異なっている・・・。

$ bundle exec rspec spec/requests/static_pages_spec.rb

Static pages
  Home page

An error occurred in an after hook
  ArgumentError: rack-test requires a rack application, but none was given
  occurred at /home/vagrant/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/capybara-2.1.0/lib/capybara/rack_test/driver.rb:16:in `initialize'

    should have the content 'Sample App' (FAILED - 1)

Failures:

  1) Static pages Home page should have the content 'Sample App'
     Failure/Error: visit '/static_pages/home'
     ArgumentError:
       rack-test requires a rack application, but none was given
     # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.00145 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:6 # Static pages Home page should have the content 'Sample App'

f:id:pyoonn:20141218194807p:plain

修正後

解決方法

spec/spec_helper.rb に下記を追記する。

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

spec/spec_helper.rb

require 'capybara/rspec'

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

# 追記した設定
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

テストコードの実行

$ bundle exec rspec spec/requests/static_pages_spec.rb

Static pages
  Home page
    should have the content 'Sample App'

Finished in 0.03796 seconds
1 example, 0 failures

f:id:pyoonn:20141218202541p:plain

参照

  1. rspec の実行で “uninitialized constant Capybara (NameError)” | Ryo's Hacks
  2. [Mac]Capybaraでハマったお話
  3. Ruby on Rails:RSpecとCapybaraで初めてのインテグレーションテスト | 何者にもなれないWebデベロッパーのメモ - whiskers