2011-04-26 167 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