0

当我在 form_tag 上方的 new.html.erb 中硬编码以下内容时,我的测试通过了:

<div class="alert alert-error">Invalid email/password combination</div>

但是,当使用 application.html.erb 中的 flash.now 消息生成 div 时,该消息将生成为 new.html.erb,测试失败。

--------------- application.html.erb ---------

<!DOCTYPE html>
<html>
<head>
  <title><%= full_title(yield(:title)) %></title>
  <%= render 'layouts/stylesheets' %>
</head>
<body>
  <a name="top" />
  <div id="main_wrapper">
    <%= render 'layouts/leaderboard_advertisement' %>
    <%= render 'layouts/header' %>
    <div id="threecolwrap">
      <div id="twocolwrap">
        <%= render 'layouts/left_advertisement' %>
        <section id="main_content_area">
          <% flash.each do | key, value | %>
            <%= content_tag(:div, value, class: "alert alert-#{key}") %>
          <% end %>          
          <%= yield %>
        </section>
      </div> <!-- end of twocolwrap -->
        <%= render 'layouts/left_advertisement' %>
    </div> <!-- end of threcolwrap -->
    <%= render 'layouts/footer' %>
  </div> <!-- end of main_wrapper -->
</body>
</html>

--------------- new.html.erb ---------

<% provide(:title, "Sign In") %>

<div>
  <div class="pages_title1">
    <h2 class='title_text_with_logo'>Sign In</h2>
    <%= image_tag "logo1_140_22.png", class: 'title_logo title_logo_position_right' %>
  </div>  
  <div class="pages_text1 wide_column">
    <div>
      <%= form_tag(sessions_path) do %>
        <%= label :session, :email %>
        <%= text_field :session, :email %>

        <%= label :session, :password %>
        <%= password_field :session, :password %>

        <%= submit_tag "Sign in" %>
  <% end %>

      <p>New user? <%= link_to "Sign up now!", signup_path %></p>
    </div>
    <div class='full_logo_center'>
      <%= image_tag "fulllogo1_200_48.png" %>
    </div>
  </div>
</div>

--------------- test.html.erb ---------

describe "signin" do
  before { visit signin_path }
  let(:page_title) { 'Sign In' }

  describe "with invalid information" do
    it { should have_selector('h2', text: 'Sign In')}
    it { should have_selector('div.alert.alert-error', text: 'Invalid') }
  end
end

--------------- session_controller.rb ---------

def create
  user = User.find_by_email(params[:session][:email].downcase)
  if user && user.authenticate(params[:session][:password])
    sign_in user
    redirect_back_or user
  else
    flash.now[:error] = "Invalid email/password combination"
    render 'new'
  end
end
4

1 回答 1

0

根据您的测试,您所做的只是访问“登录”页面。您实际上并未尝试登录,因此不会显示错误消息。

发现此类问题的最简单方法是save_and_open_page在测试开始时使用 Capybara 方法。这样,您就可以看到 Capybara 看到的页面。这使得测试中的错误非常明显。在这种情况下,您会看到没有错误消息。

于 2013-05-31T04:40:45.720 回答