RSpec 2.3 +设计1.0.11

时间:2017-06-29 06:27:09

标签: ruby-on-rails rspec devise

我有一个非常老的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并继续升级。

1 个答案:

答案 0 :(得分:1)

我认为谷歌群组中有一则旧消息是相关的:https://groups.google.com/forum/#!topic/rspec/4AHuPtHFD34

建议使用:

before do
  request.env['warden'].stub(:authenticate!) { double(User) }
end

我可能会把它放在rails_helper.rb中,以便它可以运行所有测试