2011-11-07 44 views
9

Tôi đang kiểm tra bộ điều khiển của mình bằng Rspec và dường như tôi không thể đặt biến phiên của bộ điều khiển hiện tại đang kiểm tra trước khi thực hiện yêu cầu đến đường dẫn. Ví dụ công trình này:Phiên điều khiển truy cập Rspec 2.7 trong thông số kỹ thuật trước khi yêu cầu

describe "GET /controller/path" do 
    it "if not matching CRSF should display message" do 
     get controller_path 

     request.session[:state] = "12334" 
    end 
    end 

này không làm việc (tôi nhận được một lỗi nói rằng phiên không phải là một phương pháp của lớp Nil):

 describe "GET /controller/path" do 
     it "if not matching CRSF should display message" do 
      request.session[:state] = "12334" 
      get controller_path 
     end 
     end 

Bất kỳ ý tưởng?

Trả lời

6

Hãy thử điều này:

describe "GET /controller/path" do 
    it "if not matching CRSF should display message" do 
     session[:state] = "12334" 
     get controller_path 
    end 
    end 
9

Với phiên bản mới của RSpec này được thực hiện khá tốt đẹp, tìm kiếm:

describe SessionController do 
    # routes are mapped as: 
    # match 'login' => 'session#create' 
    # get 'logout' => 'session#destroy' 

    describe "#create" do 
    context "with valid credentials" do 
     let :credentials do 
     { :email => '[email protected]', :password => 'secret' } 
     end 

     let :user do 
     FactoryGirl.create(:user, credentials) 
     end 

     before :each do 
     post '/login', credentials 
     end 

     it "creates a user session" do 
     session[:user_id].should == user.id 
     end 
    end 

    # ... 
    end 

    describe "#destroy" do 
    context "when user logged in" do 
     before :each do 
     get "/logout", {}, { :user_id => 123 } # the first hash is params, second is session 
     end 

     it "destroys user session" do 
     session[:user_id].should be_nil 
     end 

     # ... 
    end 
    end 
end 

Bạn cũng có thể sử dụng đơn giản request.session[:user_id] = 123 bên before(:each) khối, nhưng trên trông khá đẹp hơn.

+1

nó nên làm việc tại 2016) Có một lỗi khi nhận phòng này nó nên xác thực người dùng 'làm mong đợi (phiên? [: User_id].) Để eq (user.id) cuối –

Các vấn đề liên quan