1

我正在使用 RSpec 和 Capybara 测试我的 Rails 应用程序的注册表单。当我想测试它时,我遇到了这个错误,

Failures:

  1) user registration allows new users to register with an email address and password
     Failure/Error: fill_in "Confirmation",          :with => "foo"
     Capybara::ElementNotFound:
       no text field, text area or password field with id, name, or label 'Confirmation' found
     # ./user_spec.rb:9:in `block (2 levels) in <top (required)>'

这是我的表格,它是一个注册表单,

<div id="box">
  <div class="block" id="block-signup">
    <h2>Sign up</h2>
    <div class="content">
      <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => "form login"}) do |f| %>
          <% if @user.errors.any? %>
              <div id="errorExplanation">
                <h2><%= pluralize(@user.errors.count, "error") %> prohibited
                  this post from being saved:</h2>
                <ul>
                  <% @user.errors.full_messages.each do |msg| %>
                      <li><%= msg %></li>
                  <% end %>
                </ul>
              </div>
              <%end%>
        <div class="group wat-cf">
          <div class="left">
            <label class="label"><%= f.label :email %></label>
          </div>
          <div class="right">
            <%= f.email_field :email %>
            <span class="description">Ex: test@example.com</span>
          </div>
        </div>
        <div class="group wat-cf">
          <div class="left">
            <label class="label"><%= f.label :password %></label>
          </div>
          <div class="right">
            <%= f.password_field :password %>
            <span class="description">Must contains the word 'yeah'</span>
          </div>
        </div>
        <div class="group wat-cf">
          <div class="left">
            <label class="label"><%= f.label :confirmation %></label>
          </div>
          <div class="right">
            <%= f.password_field :password_confirmation %>
            <span class="description">Don't miss any letters</span>
          </div>
        </div>

        <div class="group navform wat-cf">
          <button class="button" type="submit">
            <%= f.submit "Sign in", :type => :image, :src => "http://pilu.github.com/web-app-theme/images/icons/tick.png" %>
          </button>
        </div>
      <% end %>
    </div>
    <div id="links-sup">
      <h4><%= render "links" %></h4></div>
  </div>
</div>

我的测试文件是 spec/requests/user_spec.rb

require 'spec_helper'

describe "user registration" do
  it "allows new users to register with an email address and password" do
    visit "/users/sign_up"

    fill_in "Email",                 :with => "user@eample.com "
    fill_in "Password",              :with => "foo"
    fill_in "Confirmation",          :with => "foo"

    click_button "Sign up"

    page.should have_content("Welcome! You have signed up successfully.")
  end
end

你有什么解决办法吗?

4

1 回答 1

2

好吧,这里的错误表明它找不到带有“确认”标签的文本框。在这里,您遇到了 html 问题。没有带有确认标签的文本框。您可以通过单击该标签“确认”进行测试,光标不会在password_confirmation文本框中。

看看为什么点击“密码”标签的区别,光标在密码文本框中。这就是为什么现场没有错误的原因password

您可以将标记稍微更改为f.label :password_confirmation.

当您看到错误Capybara::ElementNotFound时,仅表示它找不到您引用的元素。因此,您需要检查您的标记是否正确。

于 2012-05-28T15:00:07.833 回答