Rspecで、リクエストのテストを書こうと思っています。
expect(response).to have_http_status "200"
で確認できると思うのですが、実行してみると、
expected the response to have status code 200 but it was nil
として帰ってきてしまいます。
expect(response.status).to eq(200)
こちらのコードでも試してみたのですが、NoMethodErrorになってしまいました。
バージョンなど
ruby 2.6.2
rails 5.2.3
docker 19.03.4
##### エラー内容
Starting open-close_db_1 ... done /usr/local/bin/ruby -I/usr/local/bundle/gems/rspec-core-3.9.0/lib:/usr/local/bundle/gems/rspec-support-3.9.0/lib /usr/local/bundle/gems/rspec-core-3.9.0/exe/rspec --pattern spec/**\{,/*/**\}/*_spec.rb 2019-11-19 01:39:30 WARN Selenium [DEPRECATION] Selenium::WebDriver::Chrome#driver_path= is deprecated. Use Selenium::WebDriver::Chrome::Service#driver_path= instead. FF Failures: 1) Homes root_pathにアクセスすると 200レスポンスが帰ってくること 1 Failure/Error: expect(response).to have_http_status "200" expected the response to have status code 200 but it was nil # ./spec/requests/homes_spec.rb:7:in `block (3 levels) in <top (required)>' 2) Homes root_pathにアクセスすると 200レスポンスが帰ってくること 2 Failure/Error: expect(response.status).to eq(200) NoMethodError: undefined method `status' for nil:NilClass # ./spec/requests/homes_spec.rb:11:in `block (3 levels) in <top (required)>' Finished in 0.70259 seconds (files took 4.57 seconds to load) 2 examples, 2 failures Failed examples: rspec ./spec/requests/homes_spec.rb:5 # Homes root_pathにアクセスすると 200レスポンスが帰ってくること 1 rspec ./spec/requests/homes_spec.rb:9 # Homes root_pathにアクセスすると 200レスポンスが帰ってくること 2 /usr/local/bin/ruby -I/usr/local/bundle/gems/rspec-core-3.9.0/lib:/usr/local/bundle/gems/rspec-support-3.9.0/lib /usr/local/bundle/gems/rspec-core-3.9.0/exe/rspec --pattern spec/**\{,/*/**\}/*_spec.rb failed
コード
spec/requests/home_spec.rb
require 'rails_helper' RSpec.describe "Homes", type: :request do describe "root_pathにアクセスすると" do it "200レスポンスが帰ってくること 1" do visit root_path expect(response).to have_http_status "200" end it "200レスポンスが帰ってくること 2" do visit root_path expect(response.status).to eq(200) end end end
spec/rails_helper
require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.filter_rails_from_backtrace! config.include Capybara::DSL end
spec/spec_helper.rb
require 'capybara/rspec' RSpec.configure do |config| config.before(:each, type: :system) do driven_by :selenium_chrome_headless end config.expect_with :rspec do |expectations| expectations.include_chain_clauses_in_custom_matcher_descriptions = true end config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end config.shared_context_metadata_behavior = :apply_to_host_groups end
docker-compose.yml
version: '3' services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/myapp ports: - "3000:3000" depends_on: - db chrome: image: selenium/standalone-chrome:3.141.59-dubnium ports: - 4444:4444
Dockerfile
FROM ruby:2.6.2 RUN apt-get update -qq && \ apt-get install -y nodejs postgresql-client && \ apt-get install -y vim RUN mkdir /myapp WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock RUN bundle install COPY . /myapp # Add a script to be executed every time the container starts. COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["entrypoint.sh"] EXPOSE 3000 # Start the main process. CMD ["rails", "server", "-b", "0.0.0.0"]
gemfile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.2' gem 'rails', '~> 5.2.3' gem 'pg', '>= 0.18', '< 2.0' gem 'puma', '~> 3.11' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.2' gem 'turbolinks', '~> 5' gem 'jbuilder', '~> 2.5' gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' gem 'rspec-rails' gem 'factory_bot_rails' end group :test do gem 'capybara', '>= 2.15' gem 'selenium-webdriver' gem 'chromedriver-helper' end gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'devise' gem 'omniauth-facebook' gem 'html2slim' gem 'slim-rails', '3.1.3'
##### 試したこと
expect(page).to have_content
で、該当のページの文章が存在するかテストしてみたところ、うまく行きました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/20 02:45