Reputation: 11007
I'm having trouble testing this code in rspec - based on the error the test gives me, I know the test is written (more or less) correctly - as the data it's expecting is correct, it's just not getting it for some reason. I should also note that the code works in the browser.
edit: apologies if this was unclear. In this controller (evaluations_controller
), the user iterates through each student in a given group and evaluates their progress against a set of goals. In the new action, @student = groups.student.first
- when evaluation data for that student has been saved successfully in the create action, the student_id incremented by 1, and the new student_id is passed to the new action again (so the next student can be evaluated) - this loops until there are no more students.
What I'm trying to test is that the student_id is being successfully incremented after evaluation has been saved in the create action.
Code:
def create
...
if @evaluation.save
@id = params[:student_id]
@id = @id.to_i + 1
redirect_to evaluate_path({ student_group_id: @student_group, student_id: @id})
else
...
end
end
Rspec test:
it "should load the next student" do
#set @id to current student.id +1
@id = @student.id
@id = @id.to_i + 1
#post :create
post :create, {student_group_id: @student_group, student_id: @student, evaluation: @attr}
controller.params[:student_id].should eql @id
end
Error:
Failure/Error: controller.params[:student_id].should eql @id expected: 2 got: "1"
Upvotes: 0
Views: 591
Reputation: 35349
Your code appears to be flawed, and consequently your test is not clear.
From gleaning over the code, I understand you want to use some type of next/previous student functionality. It appears you are hacking around your controller test to achieve that.
if @evaluation.save
@id = params[:student_id]
@id = @id.to_i + 1
You are manually calculating the next id. Ask yourself this: What happens if you are working with student.id 1
, and you run this calculation, but student.id 2
has been deleted?
You get an ActiveRecord error.
You need a better way of pulling the next student. You should add an instance method to your Student model to handle that for you:
def next
Student.where(id: id).order("id ASC").first
end
In your controller you can move to the next student as such:
redirect_to evaluate_path({ student_group_id: @student_group, student_id: @student.next.id})
Then your test should be much simpler.
Upvotes: 1