*** NoMethodError例外:未定义的方法`to_sym'对于nil:Rails 3.2中的NilClass与控制器规范中的Rspec

时间:2014-05-12 09:58:16

标签: ruby-on-rails-3 rspec2 rspec-rails

我已经配置了Rspec Ruby 2.0.0-p0和Rails 3.2.14配置是完美的我当然可以确定但是当我尝试运行rake规范:控制器时,它会在我在规范示例中编写的每个请求操作上的错误下面给出错误 -

*** 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 'example@example.com'
    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

这里我在用户控制器中添加了两种测试方法。

同样的事情我尝试了演示示例,效果很好但不在我的项目中.... 感谢

2 个答案:

答案 0 :(得分:0)

如果您使用devise gem进行身份验证,则需要在控制器测试用例中指定设计映射。

使用以下代码替换before(:each)块中的代码

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

答案 1 :(得分:0)

最后我修复了问题,它来自authenticate_user!应用程序控制器中的方法,它继承了每个控制器 -

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

我已添加以下模块来解决由于此设计方法而导致的问题 - (support / 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