測試登入

spec/features/user_authentication_spec.rb

測試已存在 User 到登入頁面輸入 Email & 密碼,按下 Log in 按鈕,頁面顯示 User 的 email。

新增 spec/features/user_authentication_spec.rb

touch spec/features/user_authentication_spec.rb

並填入以下內容:

require "rails_helper"

RSpec.feature "User authentication" do
  scenario "existing user signs in" 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"

    expect(page).to have_text "[email protected]"
  end
end

跑測試:

$ rspec spec/features/user_authentication_spec.rb

.

Finished in 0.30401 seconds (files took 2.05 seconds to load)
1 example, 0 failures

測試通過因為在成功登入時,Navbar 會有 Hi!, <%= current_user.email %>

<ul class="nav navbar-nav navbar-right">
  <% if !current_user %>
    <li><%= link_to("Login", new_user_session_path) %></li>
  <% else %>
    <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown">
        Hi!, <%= current_user.email %>
        <b class="caret"></b>
      </a>
      <ul class="dropdown-menu">
        <li>
          <%= link_to("Logout", destroy_user_session_path, method: :delete) %>
        </li>
      </ul>
    </li>
  <% end %>
</ul>

results matching ""

    No results matching ""