質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ユニットテスト

ユニットテストは、システムのテスト手法の一つで、個々のモジュールを対象としたテストの事を指します。対象のモジュールが要求や性能を満たしているか確認する為に実行します。

Q&A

解決済

1回答

3622閲覧

Rspecで requestテストがうまく通らない

mkmk4423

総合スコア5

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

ユニットテスト

ユニットテストは、システムのテスト手法の一つで、個々のモジュールを対象としたテストの事を指します。対象のモジュールが要求や性能を満たしているか確認する為に実行します。

0グッド

0クリップ

投稿2019/11/19 01:50

編集2019/11/19 01:51

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
で、該当のページの文章が存在するかテストしてみたところ、うまく行きました。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

rb

visit root_path

RequestSpecではこういう場合

rb

1get root_path

のようにgetpostなどを使います。

投稿2019/11/19 07:02

Mugheart

総合スコア2344

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

mkmk4423

2019/11/20 02:45

ありがとうございます、解決できました! リクエストのテストなどは、getやpost それ以外のページの要素を取得したり操作したい場合はvititを使うのが正しいのですね、
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問