courses#show 見到單一筆資料

撰寫 "GET show" 的 test 相對簡單

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

RSpec.describe CoursesController do
  describe "GET index" do
    ...
  end

  describe "GET show" do
    it "assigns @course" do
      course = create(:course)

      get :show, params: { id: course.id }

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

    it "render template" do
      course = create(:course)

      get :show, params: { id: course.id }

      expect(response).to render_template("show")
    end
  end
end

執行測試:

$ rspec spec/controllers/courses_controller_spec.rb
..FF

Failures:

  1) CoursesController GET show assigns @course
     Failure/Error: get :show, params: { id: course.id }

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

  2) CoursesController GET show render template
     Failure/Error: get :show, params: { id: course.id }

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

Finished in 0.06085 seconds (files took 1.37 seconds to load)
4 examples, 2 failures

Failed examples:

rspec ./spec/controllers/courses_controller_spec.rb:25 # CoursesController GET show assigns @course
rspec ./spec/controllers/courses_controller_spec.rb:33 # CoursesController GET show render template

找不到 show action。

補上 action

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

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

執行測試:

$ rspec spec/controllers/courses_controller_spec.rb
..FF

Failures:

  1) CoursesController GET show assigns @course
     Failure/Error: get :show, params: { id: course.id }

     ActionController::UnknownFormat:
       CoursesController#show is missing a template for this request format and variant.

       request.formats: ["text/html"]
       request.variant: []

       NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not… nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

  2) CoursesController GET show render template
     Failure/Error: get :show, params: { id: course.id }

     ActionController::UnknownFormat:
       CoursesController#show is missing a template for this request format and variant.

       request.formats: ["text/html"]
       request.variant: []

       NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not… nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.

Finished in 0.07616 seconds (files took 1.37 seconds to load)
4 examples, 2 failures

Failed examples:

rspec ./spec/controllers/courses_controller_spec.rb:25 # CoursesController GET show assigns @course
rspec ./spec/controllers/courses_controller_spec.rb:33 # CoursesController GET show render template

補上 view

touch app/views/courses/show.html.erb

加入

<div class="col-md-12">
  <div class="course">
     <%= link_to("Edit", edit_course_path(@course) , :class => "btn btn-xs  pull-right") %>

     <h2><%= @course.title %></h2>
     <p><%= @course.description %></p>
   </div>
</div>

現在測試應該可以跑過了:

$ rspec spec/controllers/courses_controller_spec.rb
....

Finished in 0.04945 seconds (files took 1.17 seconds to load)
4 examples, 0 failures

results matching ""

    No results matching ""