RSpec 2.3 +设计1.0.11

map*_*ap7 6 rspec ruby-on-rails 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
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误;

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)>'
Run Code Online (Sandbox Code Playgroud)

错误发生在这一行;

[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
Run Code Online (Sandbox Code Playgroud)

当我在这一点时,问题是"会话"是零.

我把完整的代码推到了这里:https://github.com/map7/pdfcat/tree/devise

我的计划是让设备在测试中工作,然后我可以跳转到Rails 3.0并继续升级.

jem*_*ger 1

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

它建议使用这个:

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

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