LoginSignup
22
21

More than 5 years have passed since last update.

RailsTutorialのテスト駆動開発が上手くいかない時の対処

Posted at

はじめに

現在、RailsTutorialを勉強中ですが、3.2.1 テスト駆動開発で壮大につまずいたので、対処法を備忘録として記します。

対象バージョン

ruby 2.0.0
rails 4.2.0
rspec-core 3.1.7
rspec-rails 3.1.0

どう上手くいかないか

RailsTutorial 3.2.1テスト駆動開発をそのまま実行すると、下記の様なエラーが出る

$ bin/bundle exec rspec spec/requests/static_pages_spec.rb 
(rails_dir)/sample_app/spec/spec_helper.rb:85:in `block in <top (required)>': uninitialized constant Capybara (NameError)
...

対処方法

  1. spec/requests/static_pages_spec.rb のrequireするファイルを変更する

- require "spec_helper"
+ require "rails_helper"

参考:Everyday Rails - RSpecによるRailsテスト入門より引用

最初の行にある require 'rails_helper' に気をつけてください。そしてこれをタイプするのに慣れてください。今後すべてのスペックにこの行が含まれることになります。これはRSpec 3で新しく登場した内容です。以前のバージョンでは spec_helper と書いていました。Rails固有の設定はこのファイルに移動しています。その結果、 spec_helper はかなり小さくなりました。この内容は第9章でもう少し詳しく説明します。

2.spec/rails_helper.rbCapybara::DSLを読み込む

RSpec.configure do |config|
...
  config.include Capybara::DSL #ブロック内の最終行にでも追記
end

以上でテストが通るようになります

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

(rails_dir)/sample_app/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter (rails_dir)/sample_app/config/application.rb to limit the frameworks that will be loaded. 
### ちなみに上記はDBをmigrateしていないため出てしまいます

.

Finished in 0.17833 seconds (files took 7.04 seconds to load)
1 example, 0 failures

22
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
21