Session Access in API Specification

I am developing an API and want to test the login method. API specification located in spec/api/instead spec/controller. I am using rspec-rails. Now I have this specification:

describe "/api/login.json", :type => :api do
  let(:user) { FactoryGirl.create(:user) }

  context "when called with correct credentials" do
    before(:all) do
      post "/api/v1/passenger/login.json",:email => user.email, :password => user.password
    end

    it "responds with HTTP 200" do
      last_response.status.should == 200
    end

    it "sets the session correctly" do
      session[:user_id].should == user.id # <-- ### Here ###
    end
  end
end

He is failing. sessionequal to zero. How to access session variable?

Think I need to add some things to my block RSpec.configure?

+3
source share
2 answers

After approx. 200 hours on the Internet, I found the answer myself.

It comes down to these magic words:

module FoobarHelper
  def session
    last_request.env['rack.session']
  end
end

RSpec.configure do |c|
  c.include FoobarHelper, :type => :api
end
0
source

If your sessions are on, I think you can get it with env['rack.session'].

tut http://railscasts.com/episodes/280-pry-with-rails pry , binding.pry

+1

All Articles