使用 Devise 身份验证进行 Ruby on Rails 功能测试

2024-02-21

我正在寻找一个奇怪问题的解决方案。我有一个控制器,需要身份验证(使用 devise gem)。我添加了 Devise TestHelpers 但无法让它工作。

require 'test_helper'

class KeysControllerTest < ActionController::TestCase  
   include Devise::TestHelpers  
   fixtures :keys

   def setup
      @user = User.create!(
        :email => '[email protected] /cdn-cgi/l/email-protection',
        :password => 'MyTestingPassword',
        :password_confirmation => 'MyTestingPassword'
      )
      sign_in @user
      @key = keys(:one)
   end

   test "should get index" do
      get :index    
      assert_response :success
      assert_not_nil assigns(:keys)
   end

   test "should get new" do
      get :new
      assert_response :success
   end

   test "should create key" do
      assert_difference('Key.count') do
         post :create, :key => @key.attributes
      end

      assert_redirected_to key_path(assigns(:key))
   end

   test "should destroy key" do
      assert_difference('Key.count', -1) do
         delete :destroy, :id => @key.to_param
      end

      assert_redirected_to keys_path
   end

end

我在“rake test”窗口中得到以下输出:

29) Failure:
test_should_create_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:29]:
"Key.count" didn't change by 1.
<3> expected but was
<2>.

 30) Failure:
test_should_destroy_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:37]:
"Key.count" didn't change by -1.
<1> expected but was
<2>.

 31) Failure:
test_should_get_index(KeysControllerTest) [/test/functional/keys_controller_test.rb:19]:
Expected response to be a <:success>, but was <302>

 32) Failure:
test_should_get_new(KeysControllerTest) [/test/functional/keys_controller_test.rb:25]:
Expected response to be a <:success>, but was <302>

有人可以告诉我,为什么设计无法验证?我对 AdminController 使用完全相同的过程,并且效果完美。


您是否正在使用带有可确认功能的 Devise?在这种情况下,创建是不够的,您需要使用以下命令来确认用户@user.confirm!

其次,为什么在功能测试中创建用户?在夹具中声明您的用户,如下所示(如果您只需要确认,则为confirmed_at):

测试/装置/user.yml:

user1: 
id: 1
email: [email protected] /cdn-cgi/l/email-protection
encrypted_password: abcdef1
password_salt:  efvfvffdv
confirmed_at: <%= Time.now %>

并使用以下命令在功能测试中登录它们:

sign_in users(:user1)

编辑:我刚刚看到,在我的应用程序中,Devise-Testhelpers 在 test/test-helpers.rb 中声明,我不知道这是否有影响,也许你想尝试:

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'

class ActionController::TestCase
  include Devise::TestHelpers
end

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
  #
  # Note: You'll currently still have to declare fixtures explicitly in integration tests
  # -- they do not yet inherit this setting
  fixtures :all

  # Add more helper methods to be used by all tests here...
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Devise 身份验证进行 Ruby on Rails 功能测试 的相关文章

随机推荐

  • 实体对象不能被 IEntityChangeTracker 的多个实例引用

    我使用 EF 作为 ORM 我处置objectContext根据每一个要求 我将实体保存在缓存层中 因为我的服务获得大量流量 我有时会收到错误objectContext already disposed对于我从缓存中获取的一些实体 我已将此
  • ## 预处理器运算符有哪些应用以及需要考虑的问题?

    正如我之前的许多问题中提到的 我正在使用 K R 目前正在使用预处理器 更有趣的事情之一是我之前学习 C 的尝试中从未了解过的事情 预处理器运算符 根据 K R 的说法 预处理器运算符 提供了一种连接实际的方法 宏展开期间的参数 如果一个
  • 组件返回失败代码:0x80004005

    仅当我执行此操作时 有时才会触发此错误XMLHttpRequest uncaught exception Exception Component returned failure code 0x80004005 NS ERROR FAILU
  • Ruby - 是否可以将方法别名为安全导航运算符

    Ruby 2 3 引入了安全导航运算符 但我发现它的语法过于离散 在简单扫描代码时很容易错过 相反 我更喜欢以下语法try因为它看起来更加明显和有意 所以我的问题是在 Ruby 2 3 中 有没有办法为安全导航运算符添加别名或猴子修补方法
  • 从Python调用Matlab函数

    我有一个项目 其中有一个一个 matlab 代码 我必须运行 Django 我尝试安装 Mlabwrap 但它给了我以下错误 Traceback most recent call last File
  • 应用程序不再可用

    在移动设备上安装应用程序后 并将设备时间更改为一周前 并尝试打开应用程序它说 MyAppName Is No Longer Available 任何遇到此问题的人请告诉我 您的意见非常重要 我们有很多理由这样做 如果您没有有效的开发者许可证
  • 使用 tweepy 保存推文全文

    我是一个Python新手程序员 我在尝试使用以下命令提取一系列推文的文本时遇到问题tweepy并将其保存到文本文件 我省略了身份验证和其他内容 search api search hello count 10 textlist for i
  • 解析本地HTML文件

    我可以使用 PowerShell 解析 HTML 页面 PS gt foo Invoke WebRequest http example com PS gt foo Links Count 1 但是 如果我下载该页面 PS gt Invok
  • 结构错误中的联合

    我有以下结构 struct type1 struct type2 node union element struct type3 e int val 初始化指针时 f指向一个实例type1并做类似的事情 f element gt e甚至只是
  • 在 MATLAB 中更改 seqlogo 图形的 x 轴

    我正在制作大量seqlogos http www mathworks com access helpdesk help toolbox bioinfo ref seqlogo html以编程方式 它们有数百列宽 因此运行seqlogo通常会
  • 如何让网格为空白单元格绘制边框?

    我有一个 ItemsControl 它使用Grid as the ItemsPanelTemplate 并设置 Grid Column 和 Grid RowItemContainerStyle在网格中定位数据项 有没有办法将网格线添加到网格
  • CSS3 flexbox 调整垂直对齐元素的高度

    是否有可能使 item 1高度灵活可调 item 2高度 例如 if item 1高度是10 then item 2高度是90 if item 1高度是11 then item 2高度是89 所以根据内容 item 1我们应该调整它的大小
  • 时间:如何获得下周五?

    我怎样才能得到下周五乔达时间 http www joda org joda time API The LocalDate http www joda org joda time apidocs org joda time LocalDate
  • json_normalize JSON 文件,包含包含字典的多级列表(包含示例)

    最初是从上一个问题 https stackoverflow com questions 51236433 json normalize json file with list containing dictionary sample inc
  • HTML5 Canvas 描边未消除锯齿

    我只是想在画布上用粗的抗锯齿笔画画一个圆圈 圆圈按预期绘制 但笔划的边缘非常锯齿状 我一直读到 Chrome 强制抗锯齿 所以不知道该怎么办 Fiddle http jsfiddle net nipponese hWsxw http jsf
  • 类型错误:列表索引必须是整数或切片,而不是 str“转换字符”

    Number 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 for n in range 0 20 print Number n 1 n InputNum3 input Number I
  • VueJs - 表单提交时的 PreventDefault()

    我需要以编程方式提交表单 但我需要它preventDefault以及 现在我有以下内容 submit this refs form submit 它工作正常 但我无法阻止提交的默认设置 最终刷新页面 简短回答 您可以添加 prevent修饰
  • Qt:如何在 QAbstractItemModel 中的子表上设置标题?

    QAbstractItemModel 有一个 setHeaderData int section 方法 该方法采用行或列的部分 具体取决于标题方向 我有一个模型 其中包含几个表 这些表都是顶部项目的子项 也就是说 我的模型层次结构的第一级
  • 使用 C# 将查询插入 Paradox 表时出现问题

    我需要连接 Paradox 5 x 表才能进行选择和更新 我正在使用 OLEDBConnection 从表中选择我没有问题 在尝试插入表时 我遇到了一个问题 当我输入硬编码的字段名称时 出现错误 INSERT INTO 语句包含以下未知字段
  • 使用 Devise 身份验证进行 Ruby on Rails 功能测试

    我正在寻找一个奇怪问题的解决方案 我有一个控制器 需要身份验证 使用 devise gem 我添加了 Devise TestHelpers 但无法让它工作 require test helper class KeysControllerTe