overworkedasian
overworkedasian

Reputation: 35

devise not working properly in rails integration tests

so first off, i am using the built in integration test suite that comes with Rails, not cucumber, webrat etc. i want to master the built in suite before switching to another test suite.

using devise 1.4.2 and rails 3.1.

anyways, i am writing up a integration test that simulates a user signing up for an account, confirming the account, logging back in and filling out a form. this is where things seem to go south. from what i can tell, Devise having problems with my controller create action and because of these issues with devise, current_user is nil. here is a snippet:

require 'test_helper'

class SignupTest < ActionDispatch::IntegrationTest

test "new signup and application" do

  go_to_signup_page
  create_new_account :user => {:email => "[email protected]", :user_name => "dfdfdf",     
  :password => "dfdfdfdfdfdf", :password_confirmation => "dfdfdfdfdfdf"}
  confirm_account_creation
  go_to_signin_page
  log_in
  go_to_form
  submit_form
end

.....other tests....

def go_to_form
  get "/applications/new"
  assert_response :success
  assert_template "applications/new"
  assert_equal 'Please fill out this application', flash[:notice]
end


def submit_form
  post "/applications", :application => {.....}
  assert_response :redirect
  assert_equal 'Application Successful ', flash[:notice]
end

And in my controller i have

  before_filter :authenticate_user!

From what i determine, "def go_to_form" passes okay. But when i try to POST in "def submit_form", it tells me i need to "Please login in first or sign up", your typical devise error when trying to access a function when not logged in yet. I figure this issue is also causing current_user to be nil.

If i remove the before_filter, i am to post but current_user is still NIL.

I cant explain what is going on here. All the other actions pass with flying colors except this CREATE action.

Upvotes: 1

Views: 1103

Answers (1)

Muhammad Sannan Khalid
Muhammad Sannan Khalid

Reputation: 3137

This is working code i am using FactoryGirl, with this piece of code you can successfully logged in make sure you entered exact email and password. But there are also built in test cases in Devise gem take a look at there.

  setup do
    @user = FactoryGirl.create(:user)
  end

  def do_login
    visit '/'
    click_link "Sign in"
    fill_in 'user_email', :with => '[email protected]'
    fill_in 'user_password', :with => 'xxxx'
    click_on('sign_in')
  end

  test 'should go to section index' do
    do_login
    assert page.has_content?('Signed in successfully.')
  end

This is a factory:

FactoryGirl.define do
  factory :user do

    email "[email protected]"
    password "xxxx"
    password_confirmation "xxxx"
    :user_name "admin"
    confirmed_at "#{Time.now }"
  end
end

Upvotes: 1

Related Questions