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

[RSpec] Controllerのテスト api mode

Posted at

controllerのテストを実行する場合は、requestのテストを作成します。

$ rails generate rspec:request users

APIモードのRailsなので、基本はJSONの戻り値を判定します。

factorybotを利用した例です。
ログイン処理はshared contextで共通化しています。

requestの送り方は以下のようにきじゅつ

before { get '/users' }

require 'rails_helper'

RSpec.describe "Users", type: :request do
  let!(:user) { create(:user) }

  after { User.destroy_all }

  describe "GET /users" do
    before { get '/users' }

    it 'ユーザー覧を取得できる' do
      expect(response).to be_successful
      expect(body.size).to eq(1)
    end
  end

  describe "GET /users/:id" do
    before { get "/users/#{user.id}" }

    it 'ユーザーを取得できる' do
      expect(response).to be_successful
      pp user.id
      expect(body['id']).to eq(user.id)
    end
  end

  describe "POST /users" do
    before { post '/users', params: { user: { email: 'sample@example.com', password: "Password123!" } } }

    it 'ユーザーを作成できる' do
      expect(response).to be_successful
      expect(body['email']).to eq('sample@example.com')
    end
  end

  describe "PUT /users/'id" do
    include_context 'login setup'

    it 'ユーザーを更新できる' do
      put "/users/#{login_user.id}",
        params: { user: { email: "updated@gmail.com", password: "Password1234!" } },
        headers: login_headers

      expect(response).to be_successful
      expect(body['email']).to eq('updated@gmail.com')
    end
  end

  describe "DELETE /users/:id" do
    before { delete "/users/#{user.id}" }

    it 'ユーザーを削除できる' do
      expect(response).to be_successful
      expect(User.count).to eq(0)
    end
  end
end
0
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
0
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?