Rails – Devise User Sign In for RSpec

Last time we should you how to create the mock user using the Factory Girl ♥ Rails gem.
Rails – Create Mock Data for RSpec

Recalled that spec file.

describe "for an authorized user" do

  before(:each) do
    # Create the user object by factory
    @user = Factory(:user)
    # Sign in the yser
    test_sign_in(@user)
  end
...

 

The above codes use the test_sign_in() function which is defined in the <rails_project_root>/spec/spec_helper.rb

RSpec.configure do |config|
  def test_sign_in(user)
    user.confirm!
    controller.sign_in(user)
  end

 

I need to confirm the user before sign in because i have enabled the :confirmable setting in app/models/user.rb. otherwise you will get the following error.
Failure/Error: @user = test_sign_in(Factory(:user))
uncaught throw :warden

 

Done =)

Reference: StackOverflow – RSpec-2 and Devise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.