mardi 8 novembre 2016

Fixture reverts value after controller method

My app for teachers includes a method to delete a student from a seminar (a class but I didn't want to use the word "class") by deleting that student's record from the join table. I don't delete the student directly, but I delete an "Aula," which is a record in the student/seminar join table. I'm trying to write a test to ensure that when a student is deleted, that student's id is also deleted from the seating chart array. This function appears to be working correctly in development, but not in testing.

The Test

  test "Remove student from class period and seating chart" do
    log_in_as(@teacher_user)
    get seminar_path(@seminar)
    seating = [@student.id, @other_student.id]
    @seminar.update(:seating => seating)
    assert_difference ['Aula.count','@student.aulas.count', '@seminar.students.count'], -1 do
      delete aula_path(@aula)
    end
    puts "In test"
    puts @seminar.seating
    assert_redirected_to scoresheet_url(@seminar)
  end

The Destroy Method in the Aula Controller

def destroy
    thisAula = Aula.find(params[:id])
    @seminar = Seminar.find(thisAula.seminar_id)

    #Remove student from seating chart
    seating = @seminar.seating
    seating.delete(thisAula.student_id)
    @seminar.update(:seating => seating)
    puts "In Controller"
    puts @seminar.seating

    thisAula.destroy

    #Redirect
    flash[:success] = "Student removed from class period"
    redirect_to scoresheet_url(@seminar)
  end

Once I fix this error, I would like to include the seating chart count within the test, like so:

 assert_difference ['Aula.count','@student.aulas.count', '@seminar.students.count', '@seminar.seating.count'], -1 do

But the test fails there, because the seating array is not decreasing its count.

I've tried those "puts" lines to figure out what is happening.

Thank you in advance for any insight.

-Jeff

Running the test puts:

In Controller
614857506
In Test
45061424
614857506

This shows that the student's id is deleted from the seating array like it should be. But when the program returns to the test, the seating array has reverted to its original form, which includes the id of the deleted student.

Aucun commentaire:

Enregistrer un commentaire