測試登出
spec/features/user_sign_out_spec.rb
測試已存在 User 到登入後,按下 Logout 按鈕,頁面不顯示 User 的 email。
新增一個 scenario 到 spec/features/user_authentication_spec.rb
:
diff --git a/spec/features/user_authentication_spec.rb b/spec/features/user_authentication_spec.rb
index 29ef3b6..b68ee15 100644
--- a/spec/features/user_authentication_spec.rb
+++ b/spec/features/user_authentication_spec.rb
@@ -15,4 +15,21 @@ RSpec.feature "User authentication" do
expect(page).to have_text "[email protected]"
end
+
+ scenario "user signs out" do
+ create(:user, email: "[email protected]", password: "password")
+
+ visit "/users/sign_in"
+
+ within(".new_user") do
+ fill_in "Email", with: "[email protected]"
+ fill_in "Password", with: "password"
+ end
+
+ click_button "Log in"
+
+ click_link "Logout"
+
+ expect(page).not_to have_text "[email protected]"
+ end
end
這裡使用了 click_link
,因為 Logout
不是一個按鈕,而是一個 link。
可以使用 click_on
,則 link & button 都適用。
參考:https://github.com/teamcapybara/capybara#clicking-links-and-buttons