2011-04-26 181 views
1

我正在做Michael Hartl Rails 3教程,第8.4章316-320頁。我運行users_spec.rb測試和兩個測試不與下面的錯誤傳遞:找不到字段:「名稱」

Failures: 

1) Users signup failure should not make a new user 

Failure/Error: fill_in "Name", :with => "" 
Webrat::NotFoundError: 

Could not find field: "Name" 

# ./spec/requests/users_spec.rb:12:in `block (5 levels) in <top (required)>' 
# ./spec/requests/users_spec.rb:10:in `block (4 levels) in <top (required)>' 

2) Users signup success should make a new user 

Failure/Error: fill_in "Name",    :with => "Example User" 
Webrat::NotFoundError: 

Could not find field: "Name" 

# ./spec/requests/users_spec.rb:28:in `block (5 levels) in <top (required)>' 
# ./spec/requests/users_spec.rb:26:in `block (4 levels) in <top (required)>' 

Finished in 3.97 seconds 

2 examples, 2 failures** 

MY USERS_SPEC.RB文件 -

require 'spec_helper' 

describe "Users" do 
    describe "signup" do 
    describe "failure" do 
     it "should not make a new user" do 
     lambda do 
      visit signup_path 
      fill_in "Name",  :with => "" 
      fill_in "Email",  :with => "" 
      fill_in "Password", :with => "" 
      fill_in "Confirmation", :with => "" 
      click_button 
      response.should render_template('users/new') 
      response.should have_selector("div#error_explanation") 
     end.should_not change(User, :count) 
     end 
    end 

    describe "success" do 
     it "should make a new user" do 
     lambda do 
      visit signup_path 
      fill_in "Name",  :with => "Example User" 
      fill_in "Email",  :with => "[email protected]" 
      fill_in "Password", :with => "foobar" 
      fill_in "Confirmation", :with => "foobar" 
      click_button 
      response.should have_selector("div.flash.success",:content => "Welcome") 
      response.should render_template('users/show') 
     end.should change(User, :count).by(1) 
     end 
    end 
    end 
end 

誰能幫助我?

謝謝!

回答

0

好的我解決了這個問題。我使用了Rails 3教程使我的身份驗證系統,我自己的網站,所以我在應用程序/視圖/用戶更改註冊表單/邁克爾Hartls原新一點點:

原始 =

<div id="signupfield"> 
<%= f.label :name, "Name" %><br /> 
<%= f.text_field :name %> 
</div> 

我自己編輯 =

<div id="signupfield"> 
<%= f.label :name, "Username" %><br /> 
<%= f.text_field :name %> 
</div> 

括號(名稱&用戶名)是很重要的,因爲它會在規格/請求S/users_spec.rb:

原始 =

lambda do 
visit signup_path 
fill_in "Name", :with => ""....... 

我自己的編輯 =

lambda do 
visit signup_path  
fill_in "Username", :with => ""...... 

我得到了測試完全通過,只記得無論你放在括號必須與視圖括號相同。

0

您必須先在數據庫上創建用戶表。然後,創建相應的字段(名稱,電子郵件,密碼,確認)。

運行遷移(耙分貝:遷移)

因此,嘗試再次運行測試。

0

打印迴應以查看您實際獲得的內容。您也可以查看log/test.log以查看該請求是否存在異常或重定向。

visit signup_path 
puts response.body