【发布时间】:2017-12-02 17:19:43
【问题描述】:
我有一个非常旧的 Rails 2.3.18、ruby 1.9.3、rspec 1.x 应用程序,我们正在升级它,它有 restful-authentication。所以我用 Devise 1.0.11 替换了它。
我可以登录应用程序,但我的测试无法运行;
这是有问题的测试
require 'spec_helper'
describe CategoriesController do
context "As a logged in user" do
before do
login_user
current_firm = mock_model(Firm, :id => 1)
controller.stub!(:current_firm).and_return(current_firm)
end
describe "#index" do
it "should render index" do
get :index
response.should render_template('index')
end
end
end
end
这是我得到的错误;
NoMethodError in 'CategoriesController As a logged in user#index should render index'
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]=
/home/map7/code/pdfcat/spec/spec_helper.rb:18:in `login_user'
spec/controllers/categories_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
错误发生在这一行;
[20, 29] in /usr/local/rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/warden-0.10.7/lib/warden/session_serializer.rb
20 key
21 end
22
23 def store(user, scope)
24 return unless user
=> 25 session[key_for(scope)] = serialize(user)
26 end
问题是我此时的“会话”为零。
我已将完整代码推送到此处:https://github.com/map7/pdfcat/tree/devise
我的计划是让设计在测试中工作,然后我可以跳转到 Rails 3.0 并继续升级。
【问题讨论】:
-
我认为问题出在您的 spec_helper.rb 中的 sing_in 方法中。我想这个设计在新版本中改变了这个方法。我认为测试环境中的会话应该为零
-
sign_in 是设计的一部分,但我认为我调用它是正确的。
标签: ruby-on-rails rspec devise