2012-05-28 99 views
1

我正在測試我的Rails應用程序的RSpec和水豚註冊表單。當我想測試我面對這個錯誤,水豚:: ElementNotFound:

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: [email protected]</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> 

我的測試文件是規範/請求/ user_spec.rb

require 'spec_helper' 

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

    fill_in "Email",     :with => "[email protected] " 
    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 

你有什麼想法解決它?

回答

2

嗯,這裏的錯誤表明它找不到標籤爲「確認」的文本框。這裏你有問題的HTML。有沒有帶有確認標籤的文本框。您可以通過點擊「確認」標籤進行測試,光標將不在password_confirmation文本框中。

查看區別爲什麼點擊「密碼」標籤,光標在密碼文本框中。這就是爲什麼password字段沒有錯誤。您可以將標記更改爲f.label :password_confirmation

當您看到錯誤Capybara::ElementNotFound時,它意味着它找不到您引用的元素。因此你需要檢查你的標記是否正確。

+0

感謝您的回答,但對我來說有一個令人困惑的地方。當我在瀏覽器中查看我的應用程序html源代碼時,我看到。請問這是一個問題嗎? – ndrx42

+0

rails生成的視圖:」#{modelname} _#{fieldname}「,和「#{modelname} [#{fieldname}]」作爲名稱屬性。id使得人們可以輕鬆地從javascript訪問,並命名爲將數據提交回rails。 – Chamnap

相關問題