我是新来的MiniTest。大多数测试很容易掌握,因为它是Ruby代码,并且还具有Rspec风格的可读性。然而,我在认证方面遇到了麻烦。与任何应用程序一样,大多数控制器都隐藏在某种身份验证之后,最常见的是authenticate_user
以确保用户已登录。MiniTest身份验证
如何测试session
- >用户已登录?我从头开始不使用身份验证。
但不太清楚如何实现它。
让我们用这个作为一个例子控制器:
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.
对于索引页怎么样? – miler350
我不'看到任何区别'get(:index,{},{'user_id'=> 5})' – Oleg