我正在关注Rails Tutorial website中的示例,我遇到了使集成测试工作的问题。特别是教程中section 8.4.2中列出8.20的示例。
在下面的访问signup_path 代码行中,我收到以下错误:“未定义的局部变量或方法`signup_path'”
require 'spec_helper'
describe "Users" do
describe "signup" do
describe "failure" do
it "should not make a new user" 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
end
end
end
Here's the full test file on github
但是,如果我一次运行所有测试,那么我就不会收到错误。只有在我运行单独的测试时才会出现错误。
我的项目可以在github上查看here
如何解决此错误?
答案 0 :(得分:1)
经过一番努力之后,我意识到这根本不是一个ID(至少在Rails 3.0.3中),而是一个名为id_error_explanation
的类。
修复了将最后一位替换为:
response.should have_selector('div.id_error_explanation')
。
答案 1 :(得分:0)
您应该根据清单8.21更改测试。然后测试看起来像这样:
<强>规格/请求/ 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
get signup_path
fill_in "Name", :with => ""
fill_in "Email", :with => ""
fill_in "Password", :with => ""
fill_in "Confirmation", :with => ""
click_button "Sign up"
response.should have_selector("div#error_explanation")
end.should_not change(User, :count)
end
end
end
end