应用/控制器/ categories_controller.rb:
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /categories
# GET /categories.json
def index
@categories = Category.all
end
# GET /categories/1
# GET /categories/1.json
def show
if session[:cart] then
@cart = session[:cart]
else
@cart = {}
end
end
# GET /categories/new
def new
if current_user.admin?
@category = Category.new
end
end
# GET /categories/1/edit
def edit
if current_user.admin?
end
end
# POST /categories
# POST /categories.json
def create
if current_user.admin?
@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
end
# PATCH/PUT /categories/1
# PATCH/PUT /categories/1.json
def update
if current_user.admin?
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
end
# DELETE /categories/1
# DELETE /categories/1.json
def destroy
if current_user.admin?
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_category
@category = Category.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(:name, :desc)
end
end
在上面的控制器中,我通过检查if current_user.admin?
阻止标准用户创建,更新或销毁类别。但它在测试中引起了一些问题。
测试/控制器/ categories_controller.rb:
require 'test_helper'
class CategoriesControllerTest < ActionController::TestCase
setup do
@category = categories(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:categories)
end
test "should get new" do
get :new
assert_response :success
end
test "should create category" do
assert_difference('Category.count') do
post :create, category: { desc: @category.desc, name: @category.name }
end
assert_redirected_to category_path(assigns(:category))
end
test "should show category" do
get :show, id: @category
assert_response :success
end
test "should get edit" do
get :edit, id: @category
assert_response :success
end
test "should update category" do
patch :update, id: @category, category: { desc: @category.desc, name: @category.name }
assert_redirected_to category_path(assigns(:category))
end
test "should destroy category" do
assert_difference('Category.count', -1) do
delete :destroy, id: @category
end
assert_redirected_to categories_path
end
end
由于限制,与create,update或destroy相关的测试失败。我想我需要创建一个管理员用户并登录测试设置。但我不知道该怎么做。
我正在使用Devise gem作为应用的用户部分。
我正在使用rake test:functionals
运行我的测试。如何在我的测试中模拟用户创建和登录?
如果您想查看整个项目:https://github.com/mertyildiran/SCOR
答案 0 :(得分:1)
您可以使用灯具在测试数据库中创建管理员用户(您可能已经拥有test/fixtures/users.yml
文件),并使用设计Test helpers来登录:
sign_in :user, users(:admin)
如果您的用户可以确认,请记住设置confirmed_at
日期。