LoginSignup
11

More than 5 years have passed since last update.

ApplicationControllerとかのテストはRspecのAnonymousControllerが便利

Last updated at Posted at 2017-01-18

認証機能をApplicationControllerとかBaseControllerに実装したあと,いざテストを書こうとしてもそもそもApplicationControllerとかリクエストを受けるためのメソッドないしどうしようってなったので共有します.

そんなときはRspecのAnonymousController

base_controller_spec.rb
require 'rails_helper'

RSpec.describe BaseController, type: :controller do
  controller(BaseController) do
    def index
      render nothing: true
    end
  end

  describe '#some_method' do
    it 'should return 200' do
      get :index
      expect(response.status).to eq(200)
    end
  end
end

controller ブロックに適当なメソッドを定義するだけでそこに対してリクエストが送れるようになる. すごい. ちなみにcontrollerの引数を省略するとdescribeされているcontrollerになるらしいです.

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
11