我是新手,刚开始使用MiniTest。大多数测试很容易理解,因为它是Ruby代码,也具有类似于Rspec的可读性。然而,我在身份验证方面遇到了麻烦。与任何应用程序一样,大多数控制器都隐藏在某种身份验证后面,最常见的是
如何测试
我有这个作为参考:https://github.com/chriskottom/minitest_cookbook_source/blob/master/minishop/test/support/session_helpers.rb 但不太确定如何实现它。
让我们使用这个作为示例控制器:
authenticate_user
以确保用户已登录。如何测试
session -> user
是否已登录? 我正在使用自定义身份验证,而不是devise。我有这个作为参考: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.
include Devise::Test::IntegrationHelpers
。 - thedanotto