2017-06-29 44 views
6

我有一個非常古老的Rails 2.3.18,ruby 1.9.3,rspec 1.x應用程序,我們正在升級,它有restful認證。所以我用Devise 1.0.11取代了它。RSpec 2.3 +設計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和繼續升級。

+0

我認爲問題出在你的spec_helper.rb的sing_in方法中。我想設計在新版本中改變這種方法。我認爲這個會議應該是零測試env –

+0

sign_in是設計的一部分,但我想我正確地調用它。 – map7

回答

1

有沒有在,我認爲谷歌組的舊消息有關:https://groups.google.com/forum/#!topic/rspec/4AHuPtHFD34

它建議使用這樣的:

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

我可能把它放在rails_helper.rb所以它運行於所有測試

+0

當我把它放在我的測試文件的前面的blcok中時,我得到錯誤「無法修改凍結對象」。如果我把它放到我的rails_helper中,我得到的錯誤無法調用nilClass引用'request'對象的env。 – map7