我一直在关注Michael Hartl撰写的Ruby on Rails 3教程。直到第8章的集成测试,它一直很顺利。我运行以下命令来创建集成测试:
rails generate integration_test users
它创建了一个文件,然后我添加了2个测试;一个用于失败(空表格),一个用于成功(实际有效表格字段)。
以下是上述命令生成的users_spec.rb文件:
require 'spec_helper'
require 'database_cleaner'
describe "Users" do
describe "GET /users" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get users_index_path
response.status.should be(200)
end
end
describe "signup" do
describe "failure" do
it "should not make a new user with empty data" 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 "should not make a new user with empty data" do
end #end describe failure
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/new')
end.should change(User, :count).by(1)
end
end #end success
end #end describe signup
end #end describe Users
我不知道为什么,但所有测试都失败了。我错过了一些明显的东西吗我已经运行了rake db:migrate,清理了数据库,但我只是没有得到它。
来自rspec spec/requests/users_spec.rb
JorgeZapata:sample_app jorgezapata$ rspec spec/requests/users_spec.rb
Rack::File headers parameter replaces cache_control after Rack 1.5.
FFF
Failures:
1) Users GET /users works! (now write some real specs)
Failure/Error: get users_index_path
NameError:
undefined local variable or method `users_index_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007ffd9bbd6550>
# ./spec/requests/users_spec.rb:8:in `block (3 levels) in <top (required)>'
2) Users signup failure should not make a new user with empty data
Failure/Error: visit signup_path
ActionView::Template::Error:
/Users/jorgezapata/rails_projects/sample_app/app/views/users/new.html.erb:17: syntax error, unexpected tSTRING_BEG, expecting ')'
....label :password_confirmation "Confirmation" );@output_buffe...
... ^
# <internal:prelude>:10:in `synchronize'
# ./spec/requests/users_spec.rb:18:in `block (5 levels) in <top (required)>'
# ./spec/requests/users_spec.rb:17:in `block (4 levels) in <top (required)>'
3) Users signup success should make a new user
Failure/Error: visit signup_path
ActionView::Template::Error:
/Users/jorgezapata/rails_projects/sample_app/app/views/users/new.html.erb:17: syntax error, unexpected tSTRING_BEG, expecting ')'
....label :password_confirmation "Confirmation" );@output_buffe...
... ^
# <internal:prelude>:10:in `synchronize'
# ./spec/requests/users_spec.rb:33:in `block (5 levels) in <top (required)>'
# ./spec/requests/users_spec.rb:32:in `block (4 levels) in <top (required)>'
Finished in 0.12243 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/requests/users_spec.rb:6 # Users GET /users works! (now write some real specs)
rspec ./spec/requests/users_spec.rb:16 # Users signup failure should not make a new user with empty data
rspec ./spec/requests/users_spec.rb:31 # Users signup success should make a new user
提前致谢,请指教!
答案 0 :(得分:1)
您在错误中得到了答案。
1:
undefined local variable or method `users_index_path'
路径不存在。从控制台运行rake routes
以获取可用路由的列表。可能是users_path
。
2和3:
ActionView::Template::Error:
/Users/jorgezapata/rails_projects/sample_app/app/views/users/new.html.erb:17: syntax error, unexpected tSTRING_BEG, expecting ')'
第17行的app / views / users / new.html.erb文件中存在语法错误。
学习阅读错误输出。它告诉你需要知道的一切(通常)。