LoginSignup
5
5

More than 3 years have passed since last update.

[Rails]Request Specでのログインの実施

Posted at

はじめに

アプリ開発において、トップページ以外ログインをしていないと見れないという設定にしていたので、postsコントローラーのshowアクションのテストコードの際に利用しました。

目次

  • 1. deviseのヘルパー機能を呼び出す設定
  • 2. FactoryBotを利用したdevise認証

1. deviseのヘルパー機能を呼び出す設定

spec/rails_helper.rb
RSpec.configure do |config|
~~
  #下記を追記
  config.include Devise::Test::IntegrationHelpers, type: :request
~~
end

2. FactoryBotを利用したdevise認証

FactoryBotを利用できるように設定します。
spec配下にsupportディレクトリを作成しfactory_bot.rbファイルを作成します。

spec/support/factory_bot.rb
RSpec.configure do |config|
  config.include FactoryBot::Syntax::Methods
end

下記の一文のコメントアウトを外して有効にします。

spec/rails_helper.rb
~~
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
~~

FactoryBotを用いてテスト用のuserを作成します。

spec/factories/users.rb
FactoryBot.define do
  factory :user do
    nickname { Faker::Name.last_name }
    email { Faker::Internet.free_email }
    password = Faker::Internet.password(min_length: 6)
    password { password }
    password_confirmation { password }
    birthday { Faker::Date.in_date_period }
    gender { 2 }
  end
end

before do ~ endでユーザーログインまでの挙動を設定しています。

spec/requests/posts_request_spec.rb

require 'rails_helper'

RSpec.describe 'Posts', type: :request do
  before do
    # 登録しているuserを使うのでcreateとします。
    @user = FactoryBot.create(:user)
    # deviseのメソッドであるsign_inでログインしています。
    sign_in @user
    @post = FactoryBot.create(:post, image: fixture_file_upload('public/images/test_image.png'))
  end

  describe "GET #show" do
    it "showアクションにリクエストすると正常にレスポンスが返ってくる" do 
      get post_path(@post)
      expect(response.status).to eq 200
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミの商品名が存在する" do 
      get post_path(@post)
      expect(response.body).to include @post.name
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミのカテゴリーが存在する" do 
      get post_path(@post)
      expect(response.body).to include @post.category.name
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミの更新日が存在する" do 
      get post_path(@post)
      expect(response.body).to include @post.created_at.strftime("%Y.%m.%d")
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミの投稿者名が存在する" do 
      get post_path(@post)
      expect(response.body).to include @post.user.nickname
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミの評価が存在する" do
      get post_path(@post)
      expect(response.body).to include "#{@post.evaluation}"
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミ商品購入額が存在する" do
      get post_path(@post)
      expect(response.body).to include "#{@post.price}"
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミ商品購入店が存在する" do
      get post_path(@post)
      expect(response.body).to include @post.shop_name
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミレビュー文が存在する" do
      get post_path(@post)
      expect(response.body).to include @post.description
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミの画像が存在する" do 
      get post_path(@post)
      expect(response.body).to include 'test_image.png'
    end
    it "showアクションにリクエストするとレスポンスに投稿済みのクチコミのお気に入り数が存在する" do 
      get post_path(@post)
      expect(response.body).to include "#{@post.likes.count}"
    end
    it "showアクションにリクエストするとレスポンスに関連商品のクチコミ表示部分が存在する" do 
      get post_path(@post)
      expect(response.body).to include '関連商品のクチコミ'
    end
  end 
end

参考リンク

5
5
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
5
5