我正在尝试在轨道上进行我的第一次测试,但我遇到了麻烦。 我只是想测试一个控制器,以了解它是如何工作的,但它不起作用。 我添加了Devise助手来避免它的问题,但我仍然遇到很多错误。 我真的很感激任何帮助。
以下是测试控制器的代码:
reportes_controller_test.rb
require 'test_helper'
class ReportesControllerTest < ActionController::TestCase
include Devise::TestHelpers
setup do
@reporte = reportes(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:reportes)
end
test "should get new" do
get :new
assert_response :success
end
test "should create report" do
assert_difference('Reporte.count') do
post :create, reporte: { asignacion_actividad_id: @reporte.asignacion_actividad_id, descripcion: @reporte.descripcion}
end
assert_redirected_to reporte_path(assigns(:reporte))
end
test "should show report" do
get :show, id: @reporte
assert_response :success
end
test "should get edit" do
get :edit, id: @reporte
assert_response :success
end
test "should update report" do
patch :update, id: @reporte, reporte: { asignacion_actividad_id: @reporte.asignacion_actividad_id, descripcion: @reporte.descripcion}
assert_redirected_to reporte_path(assigns(:reporte))
end
test "should destroy report" do
assert_difference('Reporte.count', -1) do
delete :destroy, id: @reporte
end
assert_redirected_to reportes_path
end
end
以下是Controller的代码: reportes_controller.rb
class ReportesController < ApplicationController
before_action :set_reporte, only: [:show, :edit, :update, :destroy]
# asd
#
# GET /reportes
# GET /reportes.json
def index
authorize! :index, Reporte
@asignacion_actividades = AsignacionActividad.find_by(actividad_id: params[:actividad_id])
@actividad = @asignacion_actividades.actividad
@proyecto = @actividad.proyecto
@reportes_mios = []
@asignacion_actividades.to_a.each do |asignacion|
if asignacion.usuario == current_usuario && !asignacion.reportes.nil?
@reportes_mios = @reportes_mios + asignacion.reportes
end
end
@reportes_todos = @actividad.reportes
@reportes_todos = [] if @reportes_todos.nil?
@reportes_mios = [] if @reportes_mios.nil?
@reportes_todos = @reportes_todos.uniq
@reportes_mios = @reportes_mios.uniq
end
# GET /reportes/1
# GET /reportes/1.json
def show
authorize! :show, Reporte
end
# GET /reportes/new
def new
authorize! :new, Reporte
@actividad = Actividad.find(params[:actividad_id])
@proyecto = @actividad.proyecto
@asignacion_actividad = @actividad.asignacion_actividades.where('vigente =? and usuario_id =?', 'true' , current_usuario.id).uniq.last
@asignacion_actividad_id = @asignacion_actividad.id
@reporte = Reporte.new
end
# GET /reportes/1/edit
def edit
authorize! :edit, Reporte
end
# POST /reportes
# POST /reportes.json
def create
authorize! :create, Reporte
@reporte = Reporte.new(reporte_params)
respond_to do |format|
if @reporte.save
format.html { redirect_to :action => 'index', :actividad_id => @reporte.asignacion_actividad.actividad.id
flash[:notice] = 'Reporte was successfully created.' }
format.json { render :show, status: :created, location: @reporte }
else
format.html { render :new }
format.json { render json: @reporte.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reportes/1
# PATCH/PUT /reportes/1.json
def update
authorize! :update, Reporte
respond_to do |format|
if @reporte.update(reporte_params)
format.html { redirect_to @reporte, notice: 'Reporte was successfully updated.' }
format.json { render :show, status: :ok, location: @reporte }
else
format.html { render :edit }
format.json { render json: @reporte.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reportes/1
# DELETE /reportes/1.json
def destroy
authorize! :destroy, Reporte
@reporte.destroy
respond_to do |format|
format.html { redirect_to reportes_url, notice: 'Reporte was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reporte
@reporte = Reporte.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reporte_params
params.require(:reporte).permit(:descripcion, :asignacion_actividad_id)
end
end
这是我尝试执行时遇到的错误: rake test TEST = test / controllers / reportes_controller_test.rb
Run options: --seed 7969
# Running:
FFFFFFE
Finished in 0.540113s, 12.9603 runs/s, 11.1088 assertions/s.
1) Failure:
ReportesControllerTest#test_should_create_report [/home/blackswan/proyectoFinal/SEVU/test/controllers/reportes_controller_test.rb:21]:
"Reporte.count" didn't change by 1.
Expected: 3
Actual: 2
2) Failure:
ReportesControllerTest#test_should_destroy_report [/home/blackswan/proyectoFinal/SEVU/test/controllers/reportes_controller_test.rb:44]:
"Reporte.count" didn't change by -1.
Expected: 1
Actual: 2
3) Failure:
ReportesControllerTest#test_should_get_edit [/home/blackswan/proyectoFinal/SEVU/test/controllers/reportes_controller_test.rb:35]:
Expected response to be a <success>, but was <302>
4) Failure:
ReportesControllerTest#test_should_get_index [/home/blackswan/proyectoFinal/SEVU/test/controllers/reportes_controller_test.rb:11]:
Expected response to be a <success>, but was <302>
5) Failure:
ReportesControllerTest#test_should_get_new [/home/blackswan/proyectoFinal/SEVU/test/controllers/reportes_controller_test.rb:17]:
Expected response to be a <success>, but was <302>
6) Failure:
ReportesControllerTest#test_should_show_report [/home/blackswan/proyectoFinal/SEVU/test/controllers/reportes_controller_test.rb:30]:
Expected response to be a <success>, but was <302>
7) Error:
ReportesControllerTest#test_should_update_report:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"reportes", :id=>nil} missing required keys: [:id]
test/controllers/reportes_controller_test.rb:40:in `block in <class:ReportesControllerTest>'
7 runs, 6 assertions, 6 failures, 1 errors, 0 skips
我该怎么办?如果我解决了这个错误,我将能够测试所有控制器。 我会回答任何答案。谢谢你的时间。