ポートフォリオ作成も兼ねてWebアプリを作成中です。
環境ができた!と思っていたが、よくよく考えるとテスト自動化する環境を設定するのを忘れていました...
と言うことで実務で使用されていることが多いであろうRSpecを導入します。
参考にさせていただいたページ
https://qiita.com/fuqda/items/61e6b39d43733cd19802
以下手順
Gemfile
以下のgemを追加。
gemインストール。
bundle install
RSpecのインストール。
RSpecのファイルを作成。
rails g rspec:controller StatiicPages
rails g rspec:feature StatiicPages
作成されたstatic_pages_contoller_spec.rbにテストコードを記載。
require 'rails_helper'
describe "Static pages" do describe "Home page" do it "'home'と表示されること" do visit '/' expect(page).to have_content('home') expect(page).to have_title('home') end end
end
RSpecの実行。
エラーが出る...
Failures:
1) Static pages Home page homeが表示れること'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x0000563d2df1dd10>
# ./spec/controllers/statiic_pages_controller_spec.rb:6:in `block (3 levels) in <main>'
エラー対応:spec_helper.rbに以下の記載を追加 。
ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' RSpec.configure do |config| config.include Capybara::DSL
・・・(略)・・・
static_pages_contoller_spec.rbではrequire 'rails_helper'としているのだが、、原因がいまいち不明。
RSpec再実行
.
Finished in 59.24 seconds (files took 40.02 seconds to load)
1 examples, 0 failures
2019/8/11追記
Twitterにてご指摘を受けてエラーの原因がわかりました。
エラーの原因はFeatureSpecではなく、ControllerSpecで対応しようとしてたからでした。
誤 app/spec/controllers/static_pages_contoller_spec.rb
正 app/spec/features/static_pages_contoller_spec.rb
@jnchitoさん、@fuqda90さんご指摘ありがとうございました。