初めてのRSspec
導入の仕方
下記をgemファイルに記述し、bundle installします。
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'rails-controller-testing'
下記コマンドを入力します。
$ bundle exec rails generate rspec:install
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
Factory Botの設定のために、spec/rails_helper.rbの一番下に以下を記述します。
-----------------------
RSpec.configure do |config|
......
config.include FactoryBot::Syntax::Methods
end
------------------
application.rbに以下の記述を一番下に書き加えます。
config.generators do |g|
......
g.test_framework :rspec,
fixtures: true,
view_specs: false,
helper_specs: false,
routing_specs: false,
controller_specs: true,
request_specs: false
g.fixture_replacement :factory_bot, dir: "spec/factories"
end
こちらを参考に
https://qiita.com/yano12/items/eba8693272c31d6af7f0
■書いたテスト内容
view/toppages
# IndexページにToppagess#indexと表示されていること
spec/features/user_pages_spec.rb
# 新規登録ページに"新規登録"と表示されていること
spec/features/user_pages_spec.rb
テストを実行したもののエラー
$ bundle exec rspec
****
Pending: (Failures listed here are expected and do not affect your suite's status)
1) Toppages Index page should have the content 'Toppages#index'
# Feature specs require the Capybara (http://github.com/jnicklas/capybara) gem, version 2.2.0 or later. We recommend version 2.4.0 or later to avoid some deprecation warnings and have support for `config.expose_dsl_globally = false`.
# ./spec/features/toppages_spec.rb:9
2) UserPages signup page should have the content 'Sign up'
# Feature specs require the Capybara (http://github.com/jnicklas/capybara) gem, version 2.2.0 or later. We recommend version 2.4.0 or later to avoid some deprecation warnings and have support for `config.expose_dsl_globally = false`.
# ./spec/features/user_pages_spec.rb:9
Finished in 0.00716 seconds (files took 1.36 seconds to load)
2 examples, 0 failures, 2 pending
Gemfileにcapybaraを記述し、bundle install
$ bundle exec rspec
An error occurred while loading ./spec/features/user_pages_spec.rb.
Failure/Error: return load_without_bootsnap(resolved, wrap)
SyntaxError:
......
# Showing full backtrace because every line was filtered out.
# See docs for RSpec::Configuration#backtrace_exclusion_patterns and
# RSpec::Configuration#backtrace_inclusion_patterns for more information.
Finished in 0.00034 seconds (files took 1.44 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
$ bundle exec rspec
F.
Failures:
1) Toppages Index page should have the content 'Toppages#index'
Failure/Error: expect(page).to have_content "Toppages#index"
expected to find text "Toppages#index" in "新規登録 ログイン 探しものは何ですか amazonListを始める Twitterアカウントでログイン © 2019 amazonlist2."
# ./spec/features/toppages_spec.rb:10:in `block (3 levels) in <top (required)>'
Finished in 1.1 seconds (files took 1.41 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/features/toppages_spec.rb:9 # Toppages Index page should have the content 'Toppages#index'
上記に変更し再度、
$ bundle exec rspec
..
Finished in 1.07 seconds (files took 1.42 seconds to load)
2 examples, 0 failures
テストが成功した。
contentは書いてある中身全てを指すのかな。。。