我在做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**
我的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 => "user@example.com"
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 :(得分:0)
您必须首先在数据库上创建Users表。然后,创建相应的字段(名称,电子邮件,密码,确认)。
运行迁移(rake db:migrate)
所以,尝试再次运行测试。
答案 1 :(得分:0)
打印出响应以查看您实际获得的内容。您还可以查看log/test.log
以查看该请求是否存在异常或重定向。
visit signup_path
puts response.body
答案 2 :(得分:0)
好的,我解决了这个问题。我正在使用Rails 3教程为我自己的网站制作我的身份验证系统,所以我在app / views / users / new上更改了注册表格,与Michael 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>
括号(名称和用户名)很重要,因为它符合spec / requests / users_spec.rb:
<强>原始强> =
lambda do
visit signup_path
fill_in "Name", :with => "".......
我自己编辑 =
lambda do
visit signup_path
fill_in "Username", :with => ""......
我完全通过测试,只记得你在括号中放的任何内容都必须与视图括号相同。