使用 before
整理
我們也注意到
sign_in user
與 get :new
也一直重複出現。
其實我們可以用 before
來整理。
按照下面的 diff 修改 spec/controllers/courses_controller_spec.rb
:
diff --git a/spec/controllers/courses_controller_spec.rb b/spec/controllers/courses_controller_spec.rb
index a8c083e..8aff020 100644
--- a/spec/controllers/courses_controller_spec.rb
+++ b/spec/controllers/courses_controller_spec.rb
@@ -44,17 +44,16 @@ RSpec.describe CoursesController, type: :controller do
let(:user) { create(:user) }
let(:course) { build(:course) }
- it "assigns @course" do
+ before do
sign_in user
get :new
+ end
+ it "assigns @course" do
expect(assigns(:course)).to be_a_new(Course)
end
it "renders template" do
- sign_in user
- get :new
-
expect(response).to render_template("new")
end
end