Could not find field: "Name"
I am doing the Michael Hartl Rails 3 Tutorial, Chapter 8.4 pages 316-320. I run the users_spec.rb test and both tests don't pass with the following error:
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 FILE -
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_i开发者_如何学Gon "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
Can anyone help me?
Thank you!
You have to create the Users table on your database first. And then, create the respective fields (name, email, password, confirmation).
Run the migrate (rake db:migrate)
And so, try to run the tests again.
Print out the response to see what you're actually getting. You can also look at log/test.log
to see if there was an exception or redirect on that request.
visit signup_path
puts response.body
OK i solved the problem. I am using the Rails 3 Tutorial to make my authentication system for my own Website so i changed the Sign Up Form at app/views/users/new up a little bit from the original of Michael Hartls:
Original =
<div id="signupfield">
<%= f.label :name, "Name" %><br />
<%= f.text_field :name %>
</div>
My own Edit=
<div id="signupfield">
<%= f.label :name, "Username" %><br />
<%= f.text_field :name %>
</div>
The parentheses (Name & Username) are important because it goes in spec/requests/users_spec.rb:
Original=
lambda do
visit signup_path
fill_in "Name", :with => "".......
My Own Edit =
lambda do
visit signup_path
fill_in "Username", :with => ""......
I got the test to pass completely, just remember whatever you put in the parentheses has to be the same as the view parentheses.
精彩评论