2014-05-12 44 views
0

我配置了Rspec Ruby 2.0.0-p0和Rails 3.2.14配置是完美的我確信但是當我嘗試運行耙規格:控制器,它gaves我下面的錯誤寫在規範例如每請求動作 -*** NoMethodError異常:未定義的方法`to_sym'爲零:Rails 3.2中的NilClass與控制器中的Rspec規範

*** NoMethodError Exception: undefined method `to_sym' for nil:NilClass 

我寫的規格爲前控制器,但從來沒有碰到過這樣的情況,幫助我,如果任何一個已修復相同的問題....

這是我的規格和錯誤堆棧

describe UsersController do 

    before (:each) do 
    @user = FactoryGirl.create(:user) 
    sign_in @user 
    end 

    describe "GET 'index'" do 

    it "should be successful" do 
     get 'index' 
     response.should be_success 
    end 

    end 
    describe "GET 'show'" do 

    it "should be successful" do 
     get :show, :id => @user.id 
     response.should be_success 
    end 

    it "should find the right user" do 
     get :show, :id => @user.id 
     assigns(:user).should == @user 
    end 

    end 

end 

下面是結果 -

Failures: 

    1) UsersController GET 'index' should be successful 
    Failure/Error: get 'index' 
    NoMethodError: 
     undefined method `to_sym' for nil:NilClass 
    # ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>' 

    2) UsersController GET 'show' should be successful 
    Failure/Error: get :show, :id => @user.id 
    NoMethodError: 
     undefined method `to_sym' for nil:NilClass 
    # ./spec/controllers/users_controller_spec.rb:21:in `block (3 levels) in <top (required)>' 

    3) UsersController GET 'show' should find the right user 
    Failure/Error: get :show, :id => @user.id 
    NoMethodError: 
     undefined method `to_sym' for nil:NilClass 
    # ./spec/controllers/users_controller_spec.rb:26:in `block (3 levels) in <top (required)>' 

Finished in 0.4741 seconds 
3 examples, 3 failures 

Failed examples: 

rspec ./spec/controllers/users_controller_spec.rb:12 # UsersController GET 'index' should be successful 
rspec ./spec/controllers/users_controller_spec.rb:20 # UsersController GET 'show' should be successful 
rspec ./spec/controllers/users_controller_spec.rb:25 # UsersController GET 'show' should find the right user 

Randomized with seed 19701 

我廠是 -

FactoryGirl.define do 
    factory :user do 
    first_name 'Test User' 
    last_name 'Last name' 
    email '[email protected]' 
    password 'changeme' 
    password_confirmation 'changeme' 
    company 'RR' 
    confirmed_at Time.now 
    end 
end 

我們需要的公司名稱必須建立登記用戶。

class UsersController < ApplicationController 
     before_filter :authenticate_user! 
     #authorize_resource 

    def index 
     @users = User.all 
     end 

     def show 
     @user = User.find(params[:id]) 
     end 
end 

這裏我已經添加了兩種測試用戶控制器的方法。

我與偉大工程,但不是在我的項目演示的例子嘗試同樣的事情.... 感謝

+0

請分享您的規格,代碼和完整的堆棧跟蹤。 –

+0

嗨Graeme請檢查更新的問題的更多細節。謝謝 –

+0

請分享您的用戶工廠和型號。 –

回答

0

如果使用的是設計用於驗證的寶石,那麼你需要指定控制器測試用例的色器件映射。

在替換之前的代碼(:每個)與塊下面的代碼

@user = FactoryGirl.create(:user) 
@request.env['devise.mapping'] = Devise.mappings[:user] 
sign_in @user 
+0

當然我也是這樣做的。 –

+0

@RamVyevhare請用最新的代碼更新問題。 –

0

最後我固定的問題,它涉及因與authenticate_user!方法在應用控制器它繼承到每個控制器 -

prepend_before_filter :authenticate_user!, :except => [:not_authenticated] 

下面模塊來解決問題,我已經添加的發生是由於此色器件方法 - (支持/ controllers_helpers.rb)

module ControllerHelpers 
    def sign_in(user = double('user')) 
     if user.nil? 
     request.env['warden'].stub(:authenticate!). 
      and_throw(:warden, {:scope => :user}) 
     controller.stub :current_user => nil 
     else 
     request.env['warden'].stub :authenticate! => user 
     controller.stub :current_user => user 
     end 
    end 
    end 

最後包括上述模塊在spec_helper.rb文件中

config.include ControllerHelpers, :type => :controller 

運行了所有控制器規格。

乾杯!

more info

相關問題