LoginSignup
32
27

More than 5 years have passed since last update.

RspecでApplicationControllerで定義した共通メソッドのテストをする方法

Last updated at Posted at 2013-01-17

例えばログインしてるか確認する処理などbefore_filterで共通して使われそうなメソッドをテストする際に上手いことテストする方法。

Anonymous Controllerを使うと良い。

Anonymous Controllerを使うとテスト内でactionを定義することが出来るのでこのactionにリクエストを投げると良い。また、paramsの値によってテスト内容が変わる場合などもこの方法を取るとget,post呼び出し時にクエリストリングを渡す事が出来るので便利。

describe ApplicationController do
  describe "#login?" do
    controller do
      # index actionを定義
      def index
        login?
        render :text => 'dummy message'
      end
    end
    it '' do
      

      # 上で定義したactionを呼び出す
      get :index

      
    end
  end
end
32
27
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
32
27