加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – MiniTest身份验证

发布时间:2020-12-17 03:10:15 所属栏目:百科 来源:网络整理
导读:我是MiniTest的新手.大多数测试都很容易掌握,因为它是 Ruby代码,并且还具有Rspec风格的可读性.但是,我在身份验证方面遇到了麻烦.与任何应用程序一样,大多数控制器都隐藏在某种身份验证之后,最常见的是authenticate_user以确保用户已登录. 如何测试会话 – 用
我是MiniTest的新手.大多数测试都很容易掌握,因为它是 Ruby代码,并且还具有Rspec风格的可读性.但是,我在身份验证方面遇到了麻烦.与任何应用程序一样,大多数控制器都隐藏在某种身份验证之后,最常见的是authenticate_user以确保用户已登录.

如何测试会话 – >用户是否已登录?我从头开始使用身份验证而不是设计.

我有这个作为参考:https://github.com/chriskottom/minitest_cookbook_source/blob/master/minishop/test/support/session_helpers.rb

但不太确定如何实现它.

让我们使用它作为示例控制器:

class ProductsController < ApplicationController
  before_action :authenticate_user

  def index
    @products = Product.all
  end

  def show
   @product = Product.find(params[:id])
  end

end

在这些基本实例中,我的测试如何?

test "it should GET products index" do
  # insert code to check authenticate_user
  get :index
  assert_response :success
end

test "it should GET products show" do
  # insert code to check authenticate_user
  get :show
  assert_response :success
end

#refactor so logged in only has to be defined once across controllers.

解决方法

您使用自定义身份验证方法吗?
如果是这样,您可以将所需的会话变量作为第三个参数传递给请求方法:

get(:show,{'id' => "12"},{'user_id' => 5})

http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers

否则,如果您使用任何身份验证库,它通常会提供一些帮助方法进行测试.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读