我正在開發一個API並想測試登錄方法。該API的規格位於spec/api/
而不是spec/controller
。我正在使用rspec-rails。現在我有這樣的規格:在我的API規範中訪問會話
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
它失敗。 session
是零。我如何訪問會話變量?
我想我必須在我的RSpec.configure
塊中包含一些東西?
您可以試試'request.session',但我不知道它是否工作。 – Deradon 2012-04-15 15:11:37
'request'是一個由'Rack :: Test :: Methods'定義的方法,我在':type =>:api'測試的助手中包含了這個方法。這是一種類似於'post'和'get'的方法,需要一個URL作爲參數。所以'request.session'只是拋出'ArgumentError異常:錯誤的參數數量(0代表1)' – iblue 2012-04-15 15:16:09
Api :: LoginController在描述鏈中被描述得更高?我不知道是否ActionController :: TestCase :: Behavior混入。你可以訪問controller.session嗎? – oliverbarnes 2012-05-04 15:47:16