测试未通过:未定义方法“验证!”对于 nil:NilClass?

2023-12-25

我有以下失败:

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)>'

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

最奇怪的是,没有任何认证!中的方法关系_控制器_规范.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 教程 http://ruby.railstutorial.org/ruby-on-rails-tutorial-book。之后,我删除了所有身份验证系统,因为我想使用Devise.)


Setting config.include Devise::TestHelpers, :type => :controller在更高版本的 Devise 中不再起作用。您需要做的是注销匿名用户以设置正确的变量:

before :each do
  sign_out :user
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试未通过:未定义方法“验证!”对于 nil:NilClass? 的相关文章

随机推荐

  • 在 bash 中查找并删除 .txt 文件 [重复]

    这个问题在这里已经有答案了 最近 我的外部硬盘驱动器上存有我的照片 大多数都在 DVD 上 但是 因为某些分区被破坏了 幸运的是 我能够使用 PhotoRec 另一个 Unix 分区实用程序 和 PDisk 将所有内容重新组合在一起 Pho
  • 在 Dart 中监听 JS CustomEvent

    我知道我的问题并不新鲜 但我在这里和互联网上找到的所有解决方案都不起作用 或者 我正在做一些完全错误的事情 我需要在 Dart 和 JS 之间创建通信 并且我很想使用事件 因为这个想法看起来很简洁 所以 我尝试了这个教程 https dar
  • SQL Server 2008 CTE 递归

    我正在尝试使用 SQL Server 2008 的 CTE 来执行我认为是困难的递归 我似乎无法理解这个问题 在下面的示例中 您可以假设固定深度为 3 不会低于该深度 在现实生活中 深度 更深 但仍然是固定的 在这个例子中我试图简化它一些
  • Scala 集合转发器和代理的用例

    Scala 的集合库包含转发器IterableForwarder http www scala lang org api current scala collection generic IterableForwarder html Tra
  • 从python查找Windows上程序的安装目录

    python 程序需要找到 openoffice org 的安装位置 该软件安装在 Windows XP 计算机上 做这个的最好方式是什么 您可以使用 winregWindows 上的模块 首先找到注册表中的路径 例如启动regedit e
  • Docker Compose 主机名命令不起作用

    我无法获取 Docker Composehostname https docs docker com compose compose file domainname hostname ipc mac address privileged r
  • Android 中的错误 -- 无法找到以下项的检测信息:ComponentInfo

    例如 我有一个应用程序将调用联系人并且必须选择其中一个联系人 但它并没有完全按照我想要的方式做 它向我显示错误Unable to find instrumentation info for ComponentInfo com sample
  • QSplitter 在 QWidget 和 QTabWidget 之间变得无法区分

    我将 QWidget 和 QTabWidget 放在一个水平拆分器中并排放置 并且分离器失去了形状 只有将鼠标悬停在分离器上才能知道有分离器 如何让它可见 Thanks 由于 QSplitterHandle 大多数人认为是 分割器 是从 Q
  • VS 2010 Tools 的 gacutil.exe 在哪里安装程序集?

    我使用 Visual Studio 2010 命令提示符的 gacutil exe 来安装程序集 我希望将程序集添加到C WINDOWS assemblies 但它被添加到C WINDOWS Microsoft NET assembly G
  • 如何在 IntelliJ 中运行同一个应用程序两次?

    我正在使用 IntelliJ 开发我的客户端 服务器应用程序 并且刚刚发现了Compounds 基本上我可以同时运行我的客户端和服务器 并且每次我想测试时它都可以节省我无用的操作 但是 我想用 2 个客户端和 1 个服务器来测试我的应用程序
  • Java 2D:将点 P 移动到靠近另一个点一定距离?

    将 Point2D Double x 距离移近另一个 Point2D Double 的最佳方法是什么 编辑 尝试编辑 但因维护而停机 不 这不是作业 我需要将飞机 A 移向跑道末端 C 并将其指向正确的方向 角度 a 替代文本http im
  • 兑换 + 点击一次 = :-(

    我有一个普通的 Windows 窗体程序 不是 VSTO 它使用单击一次进行部署 问题是 很多用户都遇到随机错误的问题 通常会指出 由于以下错误 IClassFactory 失败 80004005 我通过将模式更改为 隔离 来部署救赎 这似
  • 以百分比形式给出省略号

    我正在尝试省略span里面的元素td元素 问题是省略号有效当且仅当我给出span元素具有固定宽度 即宽度 以像素为单位 但在我的项目中 我不能使用固定宽度span元素 这span元素必须完全拉伸到各自的内部td可以通过使用的元素width
  • Unix - 在 shell 脚本中排序

    如何根据字段位置对文件进行排序 例如 我需要对下面给定的文件进行排序 基于第 4 5 和 8 名位置 请帮忙 我尝试了以下命令 它不起作用 sort d k 3 42 44 k 4 47 57 k 5 59 70 k 8 73 82 010
  • 如何从外部功能镜头转向功能镜头

    在我开始更好地使用函数式编程的过程中 在 SO 家族成员的帮助下 我发现了什么lens https bartoszmilewski com category lens 我什至通过下面的链接对其进行了一些研究 以了解有关它们的更多信息 htt
  • 如何与剧作家一起下载?

    我正在尝试使用以下命令从网站下载文件剧作家 https github com microsoft playwright 触发下载的按钮做了一些js 然后开始下载 使用单击按钮 click函数触发下载但显示错误 失败 下载错误 我尝试过使用
  • 我可以在 Mono CSharpRepl 中重新加载程序集吗?

    因此 我有 Python 背景 现在开始在 Mac 上使用 C 和 Mono 我最近才发现Mono CSharpRepl 工具 http www mono project com CsharpRepl并希望使用它来实现与 Python 中类
  • Java中的互斥方法执行

    我有两种方法 a and b 虽然我可以接受多个线程同时访问任何方法 这是可取的 但我不希望任何线程进入a while b 正在被执行 我怎么做 Edit 1 假设有 4 个线程Thread 1正在访问A 我想要的是所有 4 个线程都不应该
  • Angular REST API 安全性

    当我使用 Angular 消费 REST API 请求时 我的应用程序出现问题 Web 服务 URL 存储在 Angular 服务或控制器 js 文件中 因此 如果我有登录网络服务来检查用户名和密码 例如 最终用户或开发人员可以获取此网址并
  • 测试未通过:未定义方法“验证!”对于 nil:NilClass?

    我有以下失败 Failures 1 RelationshipsController creating a relationship with Ajax should increment the Relationship count Fail