使用者必須登入才能操作 course#edit

修改 app/contrllers/courses_controller

class CoursesController < ApplicationController

-  before_action :authenticate_user!, :only => [:new, :create]
+  before_action :authenticate_user!, :only => [:new, :create, :edit]

跑測試

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

Failures:

  1) CoursesController GET edit assign course
     Failure/Error: expect(assigns[:course]).to eq(course)

       expected: #<Course id: 1, title: "Course Name", description: "Description.", created_at: "2016-12-11 16:19:58", updated_at: "2016-12-11 16:19:58", user_id: 1>
            got: nil

       (compared using ==)

  2) CoursesController GET edit render template
     Failure/Error: expect(response).to render_template("edit")
       expecting <"edit"> but rendering with <[]>

Finished in 0.29661 seconds (files took 1.67 seconds to load)
22 examples, 2 failures

修改 spec/controllers/courses_controller_spec.rb 中的 GET edit

diff --git a/spec/controllers/courses_controller_spec.rb b/spec/controllers/courses_controller_spec.rb
index aa09d36..4190bfa 100644
--- a/spec/controllers/courses_controller_spec.rb
+++ b/spec/controllers/courses_controller_spec.rb
@@ -113,6 +113,9 @@ RSpec.describe CoursesController, type: :controller do
   end

   describe "GET edit" do
+    let(:user) { create(:user) }
+    before { sign_in user }
+
     it "assign course" do
       course = create(:course)

在 edit 測試執行前創建 user 並登入即可。

results matching ""

    No results matching ""