选择器位于application.html.erb中时,Rails has_selector不起作用

时间:2013-05-31 04:10:41

标签: ruby-on-rails-3.2

当我在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

1 个答案:

答案 0 :(得分:0)

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

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