LoginSignup
9
9

More than 5 years have passed since last update.

Rspecのrequest(Integration test)でDeviseを使う方法

Posted at

目的

以下の2点により、本記事をまとめました。

  • いろいろググって試したりしたけど、なんだかうまくいかなかった
  • Deviseの公式を翻訳してみたけど、公式としてIntegration Testはできないよって書いてあった

参考にしたところ

最終的には、このサイトの通りでうまく行きました。

やったこと

  1. spec/spec_helper.rbRSpec.configure do |config|のところに以下を追加した。

    spec/spec_helper.rb
    RSpec.configure do |config|
    
      # Devise
      config.include Warden::Test::Helpers
      config.before :suite do
        Warden.test_mode!
      end
    
    end
    
  2. FactoryGirlの設定

    spec/factories.rb
    FactoryGirl.define do
      factory :user do
        email 'test@example.com'
        password 'passworddayo'
      end
    end
    
  3. request specのbefore処理でログインする

    spec/request/some_request_spec.rb
      let(:user) { FactoryGirl.create(:user) }
      before do
        login_as(user, :scope => :user)
    
        visit some_authentificated_path
      end 
    
9
9
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
9
9