0
我的Rails代碼如下:如何將會話值分配給Rspec控制器中的實例變量?
class MyController < ApplicationController
def index
@session_is_admin = session[:user_permission][:is_admin]
begin
@obj = Class.all
respond_to do |format|
format.html # index.html.erb
end
rescue
end
end
end
Rspec的代碼:根據(rspec的/控制器)
require 'spec_helper'
describe MyController do
describe "GET index" do
it "populates an array of data" do
get :index
session[:user_permission][:is_admin] = false
assigns(:session_is_admin).should eq(false)
end
end
end
執行它爲我提供經過以下錯誤:
1) MyController GET index populates an array of data
Failure/Error: get :index
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/my_controller.rb:16:in `index'
# ./spec/controllers/my_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
Finished in 9.27 seconds
1 example, 1 failure
請幫我解決上述問題。這是由於我的rspec代碼沒有成功完成會話值分配。
嘗試加入'會話[:user_permission] = {}'分配之前'FALSE' – tihom
嘗試使用session_is_admin在@session_is_admin –