在 course#edit 修改表單

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

describe "GET edit" do
  it "assign course" do
    course = create(:course)

    get :edit , :id => course.id

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

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

    get :edit , :id => course.id

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

跑測試:

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

Failures:

  1) CoursesController GET edit assign course
     Failure/Error: get :edit, params: { :id => course.id }

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

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

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

Finished in 0.12654 seconds (files took 1.23 seconds to load)
12 examples, 2 failures

Failed examples:

rspec ./spec/controllers/courses_controller_spec.rb:95 # CoursesController GET edit assign course
rspec ./spec/controllers/courses_controller_spec.rb:103 # CoursesController GET edit render template

會發現找不到 action。

補上 action

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

def edit
  @course = Course.find(params[:id])
end
$ rspec spec/controllers/courses_controller_spec.rb
..........FF

Failures:

  1) CoursesController GET edit assign course
     Failure/Error: get :edit, params: { :id => course.id }

     ActionController::UnknownFormat:
       CoursesController#edit 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 edit render template
     Failure/Error: get :edit, params: { :id => course.id }

     ActionController::UnknownFormat:
       CoursesController#edit 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.1319 seconds (files took 1.24 seconds to load)
12 examples, 2 failures

Failed examples:

rspec ./spec/controllers/courses_controller_spec.rb:95 # CoursesController GET edit assign course
rspec ./spec/controllers/courses_controller_spec.rb:103 # CoursesController GET edit render template

找不到 view。

補上 view

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

加入

<div class="col-md-12">
  <%= form_for @course do |f| %>
    <%= f.text_field :title %>
    <%= f.text_area :description %>
    <div class="form-actions">
       <%= f.submit "Submit", :disable_with => 'Submiting...', :class => "btn btn-primary"%>
    </div>
  <% end %>
</div>

測試通過!

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

Finished in 0.10362 seconds (files took 1.21 seconds to load)
12 examples, 0 failures

results matching ""

    No results matching ""