courses#destroy 刪除資料
編輯 spec/controllers/courses_controller_spec.rb
,加入另一段 Test。
describe "DELETE destroy" do
it "assigns @course" do
course = create(:course)
delete :destroy, id: course.id
expect(assigns[:course]).to eq(course)
end
it "deletes a record" do
course = create(:course)
expect { delete :destroy, id: course.id }.to change { Course.count }.by(-1)
end
it "redirects to courses_path" do
course = create(:course)
delete :destroy, id: course.id
expect(response).to redirect_to courses_path
end
end
$ rspec spec/controllers/courses_controller_spec.rb
.................FFF
Failures:
1) CoursesController DELETE destroy assigns @course
Failure/Error: delete :destroy, params: { id: course.id }
AbstractController::ActionNotFound:
The action 'destroy' could not be found for CoursesController
2) CoursesController DELETE destroy deletes a record
Failure/Error: expect { delete :destroy, params: { id: course.id } }.to change { Course.count }.by(-1)
AbstractController::ActionNotFound:
The action 'destroy' could not be found for CoursesController
3) CoursesController DELETE destroy redirects to courses_path
Failure/Error: delete :destroy, params: { id: course.id }
AbstractController::ActionNotFound:
The action 'destroy' could not be found for CoursesController
Finished in 0.18993 seconds (files took 1.23 seconds to load)
20 examples, 3 failures
Failed examples:
rspec ./spec/controllers/courses_controller_spec.rb:160 # CoursesController DELETE destroy assigns @course
rspec ./spec/controllers/courses_controller_spec.rb:168 # CoursesController DELETE destroy deletes a record
rspec ./spec/controllers/courses_controller_spec.rb:174 # CoursesController DELETE destroy redirects to courses_path
補上 action
在 app/controllers/courses_controller.rb
加入 destroy
這個 action:
def destroy
@course = Course.find(params[:id])
@course.destroy
redirect_to courses_path
end