1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】Rspecレスポンステストについて

Posted at

はじめに

※独学で学習しているので間違いやもっといい方法がある場合があると思います。
そのような時はご教授いただけると大変嬉しいです!

概要

Rspecでのテストコードの書き方をまとめてみました。(レスポンステスト編になります。)

以下の記事を参考に学習しました。
RailsでRSpecの初期設定を行う際のテンプレートを作ってみる-Qiita
Rails チュートリアル(3章、4章、5章)をRSpecでテスト-Qiita

1.Rspecのセットアップ

Gemをインストール

gemfileにRspecを追記

gemfile
group :development, :test do
  gem 'rspec-rails', '~> 4.0.1'
end

次にGemをインストールします。

$ bundle install

railsコマンドで rspecをプロジェクトにインストールします。

$ rails generate rspec:install

rspecの設定編集

.rspec
--require rails_helper
--format documentation
config/application.rb
module RailsTutorial
  class Application < Rails::Application
    config.load_defaults 5.2
    
    # ↓追加
    # テストフレームワークにrspecを指定することで、rails g ~を実行した際、自動的にspecファイルも作成する設定
    config.generators do |g|
      g.test_framework :rspec,
                       helper_specs: false,
                       routing_specs: false,
                       view_specs: false,
                       controller_specs: false
    end
    # ↑追加

  end
end


2.Rspecでテストを書いてみる

Rspecファイルの作成する

Rspecではテストコードのことを'スペック'と呼ばれているらしいです。
(コントローラーのテストコードのことは’コントローラースペック’みたいな感じらしいです。)
※ファイルの名は「(コントローラ名,モデル名)_spec.rb」のように命名する。

Railsの場合はrails generateコマンドでファイルを作成できます。

# controllerの場合
$ rails g rspec:controller ファイル名

# modelの場合
$ rails g rspec:model ファイル名

$ rails g rspec:controller StaticPages

テストコードを記述する

spec/requests/static_pages_request_spec.rb
require 'rails_helper'

RSpec.describe 'Access to static_pages', type: :request do

	# homeページへのリクエスト送信テスト
	context 'GET #home' do
		before { get static_pages_home_path }

		# リクエストに対するレスポンステスト
		it 'responds successfully' do
			expect(response).to have_http_status 200
		end
	end

end

describeとかcontextとかよくわからない。

一つ一つみていきましょう。

・describe,type

_spec.rb

RSpec.describe 'Access to static_pages', type: :request do

end



RSpec.describe [テスト名], type: [Specの種類] do

end

# Specの種類
# 今回はレスポンステストなのでrequestになります。
# 他には'system','controller','model','routing','view'などがあるみたいです。

・context,before,it

_spec.rb
RSpec.describe 'Access to static_pages', type: :request do
    context 'GET #home' do
        before { get static_pages_home_path }

        # リクエストに対するレスポンステスト
        it 'responds successfully' do
            expect(response).to have_http_status 200
        end
    end
end

  ↓

RSpec.describe 'Access to static_pages', type: :request do
    context ['~の場合'(お好きな名前で)] do
        before [事前準備]

        # リクエストに対するレスポンステスト
        it ['仕様の内容'(お好きな名前で)] do
            [期待する動作]
        end
    end
end

上から順番に読んでいくと
itの中でようやくテストコードが記載されています。
describe>context>...を何個もネストしていく構造でテストコードが書かれています。
※describeの中にdescribeをネストすることは可能ですが、typeの指定は一番外側のdescribeでのみ行います。

spec/requests/static_pages_request_spec.rb
require 'rails_helper'

RSpec.describe 'Access to static_pages', type: :request do

	# homeページへのリクエスト送信テスト
	context 'GET #home' do
		before { get static_pages_home_path }

		# リクエストに対するレスポンステスト
		it 'responds successfully' do
			expect(response).to have_http_status 200
		end
	end

end

expect(response).to have_http_status 200

これはbeforeで{ get static_pages_home_path }つまり
homeのパスにGETメソッドで通信を行った時に、返ってくるレスポンスが200(OK)ならテスOKという意味です。

以上、簡単にですがRailsでのRspecのレスポンステストの書き方についてです。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?