Sorcery等によって提供されるcurrent_user、require_loginを使ったrailsアプリを、rspecでテストする方法を共有します。
実現方法
current_user
のスタブを作ります。
テストの記述方法
items_controller_spec.rb
before do
@user = User.first
allow(controller)
.to receive(:current_user)
.and_return(@user)
end
説明
controller
にcurrent_user
のスタブを作ることによって、before_action require_login
を通過できます。
以上です。
ログイン機能を実際に経由して本物のcurrent_user
を呼び出す方法を確立している方がいらっしゃいましたら、方法を共有して頂けると嬉しいです。
http://stackoverflow.com/questions/5865555/how-to-do-integration-testing-with-rspec-and-devise-cancan
http://stackoverflow.com/questions/3720953/rspec-accessing-application-controller-methods-such-as-current-user