courses#update 送出表單

編輯 spec/controllers/courses_controller_spec.rb,加入另一段 Test。

describe "PUT update" do
  it "assign @course" do
    course = create(:course)

    put :update , params: { id: course.id, course: { title: "Title", description: "Description" } }

    expect(assigns[:course]).to eq(course)
  end

  it "changes value" do
    course = create(:course)

    put :update , params: { id: course.id, course: { title: "Title", description: "Description" } }

    expect(assigns[:course].title).to eq("Title")
    expect(assigns[:course].description).to eq("Description")
  end

  it "redirects to course_path" do
    course = create(:course)

    put :update , params: { id: course.id, course: { title: "Title", description: "Description" } }

    expect(response).to redirect_to course_path(course)
  end
end
$ rspec spec/controllers/courses_controller_spec.rb
............FFF

Failures:

  1) CoursesController PUT update assign @course
     Failure/Error: put :update , params: { id: course.id, course: { title: "Title", description: "Description" } }

     AbstractController::ActionNotFound:
       The action 'update' could not be found for CoursesController

  2) CoursesController PUT update changes value
     Failure/Error: put :update , params: { id: course.id, course: { title: "Title", description: "Description" } }

     AbstractController::ActionNotFound:
       The action 'update' could not be found for CoursesController

  3) CoursesController PUT update redirects to course_path
     Failure/Error: put :update , params: { id: course.id, course: { title: "Title", description: "Description" } }

     AbstractController::ActionNotFound:
       The action 'update' could not be found for CoursesController

Finished in 0.14292 seconds (files took 1.25 seconds to load)
15 examples, 3 failures

Failed examples:

rspec ./spec/controllers/courses_controller_spec.rb:113 # CoursesController PUT update assign @course
rspec ./spec/controllers/courses_controller_spec.rb:121 # CoursesController PUT update changes value
rspec ./spec/controllers/courses_controller_spec.rb:130 # CoursesController PUT update redirects to course_path

補上 action

app/controllers/courses_controller.rb 加入 update 這個 action

def update
  @course = Course.find(params[:id])
  @course.update(course_params)

  redirect_to course_path(@course)
end

繼續測試沒有 title 的狀況

describe "PUT update" do
  context "when course has title" do
    it "assigns @course" do
      course = create(:course)

      put :update, params: { id: course.id, course: { title: "Title", description: "Description" } }

      expect(assigns[:course]).to eq(course)
    end

    it "changes value" do
      course = create(:course)

      put :update, params: { id: course.id, course: { title: "Title", description: "Description" } }

      expect(assigns[:course].title).to eq("Title")
      expect(assigns[:course].description).to eq("Description")
    end

    it "redirects to course_path" do
      course = create(:course)

      put :update, params: { id: course.id, course: { title: "Title", description: "Description" } }

      expect(response).to redirect_to course_path(course)
    end
  end

  context "when course doesn't have title " do
    it "doesn't update a record " do
      course = create(:course)

      put :update, params: { id: course.id, course: { title: "", description: "Description" } }

      expect(course.description).not_to eq("Description")
    end

    it "renders edit template" do
      course = create(:course)

      put :update, params: { id: course.id, course: { title: "", description: "Description" } }

      expect(response).to render_template("edit")                
    end
  end
end
$ rspec spec/controllers/courses_controller_spec.rb
................F

Failures:

  1) CoursesController PUT update when course doesn't have title  renders edit template
     Failure/Error: expect(response).to render_template("edit")
       expecting <"edit"> but rendering with <[]>
     # ./spec/controllers/courses_controller_spec.rb:154:in `block (4 levels) in <top (required)>'

Finished in 0.1161 seconds (files took 1.5 seconds to load)
17 examples, 1 failure

Failed examples:

rspec ./spec/controllers/courses_controller_spec.rb:149 # CoursesController PUT update when course doesn't have title  renders edit template

修改 update action

修改 app/controllers/courses_controller.rb update 這個 action

def update
  @course = Course.find(params[:id])

  if @course.update(course_params)
    redirect_to course_path(@course)
  else
    render :edit
  end
end
$ rspec spec/controllers/courses_controller_spec.rb
.................

Finished in 0.10993 seconds (files took 1.49 seconds to load)
17 examples, 0 failures

results matching ""

    No results matching ""