Accessing the session within the specification

I have this simple helper found in spec / helpers / session.rb:

def sign_in user
    session[:remember_token] = user.remember_token
end

      

However, I get an error when I try to use it in the spec. Following:

context 'when user is is signed in' do
    before do 
        sign_in user
        request 
    end

    specify{ expect(flash[:success]).to eq "Signed out successfully" }
end

      

gives me:

Failure/Error: sign_in user
NameError:
undefined local variable or method `session' for #<RSpec::ExampleGroups::AdminArea::Authentication::GuestVisitsRoot:0x00000004f835f0>

      

So how can I manipulate the session from within the spec? Is it possible?

+3


source to share


1 answer


I believe you are seeing this error in your integration test. Integration tests are not meant to test related things.

You can achieve the same result

def sign_in user
    # you can also use 
    # something like this 
    # post signin_path, :login => user.login, :password => 'password'
    # but i dont recommend this, since this is not how user do it.
    visit sign_in_path
    fill_in 'Email', with: user.email
    fill_in 'Password', with: user.password
    click_button 'Sign in'
end 

      

And then in the test, something like



 expect(page).to have_content('My Account')

      

There are many great answers. See if you have any problems .. include it in the question, I will try to answer

+6


source







All Articles