2014-10-30 5 views
0

레일 테스트를 처음하고 있지만 문제가 있습니다. 어떻게 작동하는지 이해하기 위해 하나의 컨트롤러를 테스트하려고하는데 작동하지 않습니다. 문제를 피하기 위해 Devise 도우미를 추가했지만 여전히 많은 오류가 발생합니다. 정말 많은 도움을 주시면 감사하겠습니다. 여기레일 컨트롤러 테스트가 작동하지 않습니다.

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 

그리고이입니다 reportes_controller.rb : 컨트롤러의 코드

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 

됩니다 : 여기

는 테스트 컨트롤러의 코드 내가 그것을 실행하려고 할 때 얻을 수있는 오류 : rake test TEST = 테스트/컨트롤러/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 

어떻게해야합니까? 이 오류를 해결하면 모든 컨트롤러를 테스트 할 수 있습니다. 나는 모든 대답을 기각 할 것이다. 시간 내 주셔서 감사합니다.

답변

0

인증에 대해 생각 하시겠습니까? 귀하의 컨트롤러가 잘 작동합니까?

괜찮 으면 RedMine을 참조하십시오. 참조 할만한 많은 테스트 사례가 있습니다.

+0

컨트롤러가 작동 중입니다. 모든 방법이 작동합니다. 고마워요 – Niemand

+0

나는 권한에 대해 모두 확인하지만 괜찮아 보인다. 더 이상 무엇이 될 수 있습니까? – Niemand

관련 문제