Rails-当我使用before_action时,如何通过控制器测试



我需要运行一些测试,但我在这里停滞不前。

我正在约会控制器中使用before_action

这是控制器

class AppointmentsController < ApplicationController
before_action :set_appointment, only: %i[ show edit update destroy ]
#before we run anything if the user is not signed in show index and show functions
before_action :authenticate_user!, except: [:index,:show]
#only the correct user can edit,update and destroy
before_action :correct_user, only: [:edit, :update , :destroy]
# GET /appointments or /appointments.json
def index
@appointments = Appointment.all.decorate
end
# GET /appointments/1 or /appointments/1.json
def show
end
# GET /appointments/new
def new
#@appointment = Appointment.new
@appointment = current_user.appointments.build
end
# GET /appointments/1/edit
def edit
end
#function to allow for search functionality 
def search
@appointments = Appointment.where("date LIKE?", "%"+params[:q]+"%")
end
# POST /appointments or /appointments.json
def create
#@appointment = Appointment.new(appointment_params)
@appointment = current_user.appointments.build(appointment_params)
#here underneath I am using my custom gem to filter bad words within the notes field when creating an appointment
@appointment.notes = Badwordgem::Base.sanitize(@appointment.notes)
respond_to do |format|
if @appointment.save
format.html { redirect_to appointment_url(@appointment), notice: "Appointment was successfully created." }
format.json { render :show, status: :created, location: @appointment }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /appointments/1 or /appointments/1.json
def update
respond_to do |format|
if @appointment.update(appointment_params)
format.html { redirect_to appointment_url(@appointment), notice: "Appointment was successfully updated." }
format.json { render :show, status: :ok, location: @appointment }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /appointments/1 or /appointments/1.json
def destroy
@appointment.destroy
respond_to do |format|
format.html { redirect_to appointments_url, notice: "Appointment was successfully destroyed." }
format.json { head :no_content }
end
end
#function here that restricts editing so the current logged in user can edit only their records
def correct_user
@appointment = current_user.appointments.find_by(id: params[:id])
redirect_to appointments_path, notice:"NOT ALLOWED TO EDIT THIS" if @appointment.nil?
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
@appointment = Appointment.find(params[:id])
end
# Only allow a list of trusted parameters through.
def appointment_params
params.require(:appointment).permit(:barber, :customer, :notes, :date,:user_id)
end
end

这是我的测试控制器

require "test_helper"
class AppointmentsControllerTest < ActionDispatch::IntegrationTest
Devise::Test::IntegrationHelpers
setup do
@appointment = appointments(:one)
end
test "should get index" do
get appointments_url
assert_response :success
end
test "should get new" do
get new_appointment_url
assert_response :success
end
test "should create appointment" do
assert_difference('Appointment.count') do
post appointments_url, params: { appointment: { barber: @appointment.barber, customer: @appointment.customer, date: @appointment.date, notes: @appointment.notes } }
end
assert_redirected_to appointment_url(Appointment.last)
end
test "should show appointment" do
get appointment_url(@appointment)
assert_response :success
end
test "should get edit" do
get edit_appointment_url(@appointment)
assert_response :success
end
test "should update appointment" do
patch appointment_url(@appointment), params: { appointment: { barber: @appointment.barber, customer: @appointment.customer, date: @appointment.date, notes: @appointment.notes } }
assert_redirected_to appointment_url(@appointment)
end
test "should destroy appointment" do
assert_difference('Appointment.count', -1) do
delete appointment_url(@appointment)
end
assert_redirected_to appointments_url
end
end

如果我评论掉";动作之前";在我的控制器中,当然所有的测试都通过了,但其中有15个测试失败了。如何使用before_action使测试通过?

对于:authenticate_user,在创建用户后,只需在te测试中使用助手log_in,如下所示:

class AppointmentsControllerTest < ActionDispatch::IntegrationTest
let(:user) { User.new(user_params) }
......

并放置

sign_ig user

在每个"你想要的it方法"中

对于:set_appointment:correct_user,只在路径的调用内传递正确的id参数

最新更新