Alberto Pellizzon
Alberto Pellizzon

Reputation: 609

Stub current_user when testing devise views

I am testesting my custom devise views with capybara and i need a logged in user in order to tests some views, for example:

feature 'user/edit' do
  describe "the edit profile process", :type => :feature do
    include Devise::TestHelpers

    let!(:user){ FactoryGirl.build(:user)

    def sign_in(user)
      if user.nil?
        request.env['warden'].stub(:authenticate!).
          and_throw(:warden, {:scope => :user})
          controller.stub :current_user => nil
      else
        request.env['warden'].stub :authenticate! => user
         controller.stub :current_user => user
      end
    end

    it "update " do
      sign_in(user)
      visit '/users/edit'
      within("#edit_user") do  
         #=> something
      end
      click_on 'Update'
      page.should have_content("something")
    end
  end
end

But i got this error

Failure/Error: request.env['warden'].stub :authenticate! => user

NoMethodError: undefined method `env' for nil:NilClass

Upvotes: 1

Views: 1608

Answers (1)

Peter Alfvin
Peter Alfvin

Reputation: 29379

The module Devise::TestHelpers establishes the @request instance variable. You are referencing request, which is apparently set to nil in some other accessible scope, resulting in the error you're getting.

See https://github.com/plataformatec/devise/blob/b8ed2f31608eccb6df6d5bb2e66238d6dfc4bcfc/lib/devise/test_helpers.rb for the source of TestHelpers.

Upvotes: 1

Related Questions