【问题标题】:Test not passing: undefined method `authenticate!' for nil:NilClass?测试未通过:未定义的方法“验证!”对于零:NilClass?
【发布时间】:2014-01-16 23:50:32
【问题描述】:

我有以下失败:

Failures:

  1) RelationshipsController creating a relationship with Ajax should increment the Relationship count
     Failure/Error: xhr :post, :create, relationship: { followed_id: other_user.id }
     NoMethodError:
       undefined method `authenticate!' for nil:NilClass
     # ./spec/controllers/relationships_controller_spec.rb:14:in `block (4 levels) in <top (required)>'
     # ./spec/controllers/relationships_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

但是很奇怪,如果我访问该站点,事情就会起作用(如果我单击 Follow 按钮,followers 计数器会增加:

最奇怪的是没有任何身份验证! relationships_controller_spec.rb中的方法:

require 'spec_helper'

describe RelationshipsController do

  let(:user) { FactoryGirl.create(:user) }
  let(:other_user) { FactoryGirl.create(:user) }

  before { sign_in user }

  describe "creating a relationship with Ajax" do

    it "should increment the Relationship count" do
      expect do
        xhr :post, :create, relationship: { followed_id: other_user.id }
      end.to change(Relationship, :count).by(1)
    end

    it "should respond with success" do
      xhr :post, :create, relationship: { followed_id: other_user.id }
      response.should be_success
    end
  end

  describe "destroying a relationship with Ajax" do

    before { user.follow!(other_user) }
    let(:relationship) { user.relationships.find_by_followed_id(other_user) }

    it "should decrement the Relationship count" do
      expect do
        xhr :delete, :destroy, id: relationship.id
      end.to change(Relationship, :count).by(-1)
    end

    it "should respond with success" do
      xhr :delete, :destroy, id: relationship.id
      response.should be_success
    end
  end
end

不在控制器中:

class RelationshipsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end
end

可能是什么问题?

(顺便说一下,这些测试是在Ruby on Rails Tutorial之后进行的。之后,我删除了所有的身份验证系统,因为我想使用Devise。)

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    设置config.include Devise::TestHelpers, :type =&gt; :controller 不再适用于更高版本的设计。您需要做的是注销匿名用户以设置正确的变量:

    before :each do
      sign_out :user
    end
    

    【讨论】:

    • 如果这不再有效,为什么它仍然在 gem 中读我? github.com/plataformatec/devise#test-helpers
    • 这个答案对我有用。但我无意中注意到我在某些规范文件中有一个额外的include Devise::TestHelpersspec_helper.rb 中的那个除外),删除它解决了问题。希望这对某人有所帮助。
    • @JCC 它仍在 gems 自述文件中,因为大多数开源开发人员都有我们需要完成的日常工作。
    • @freemaniod 在我的配置中也发现了一个重复的包含,删除它解决了这个问题,谢谢!
    【解决方案2】:

    我必须添加这个:

    spec_helpers.rb:

    RSpec.configure do |config|
      config.include Devise::TestHelpers, :type => :controller
    end
    

    【讨论】:

    • 太棒了。很困惑为什么我的控制器规格中出现随机错误,而不是单元或请求。谢谢!
    • @papirtiger 如果上述方法不起作用,是否有解决方案,这似乎对我不起作用。
    • @John 该行应包含在require 'rspec/rails' 之后。我把它放在spec/rails_helper.rb 中,它可以工作。
    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多