2010-10-02 71 views
1

我正在关注Rails Tutorial website中的示例,并且遇到了使集成测试正常工作的问题。特别是本教程中section 8.4.2中列表8.20的示例。路由在RSpec集成测试中不可用

在低于访问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

您应该根据清单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 
+0

消除行'response.should render_template('users/new')'摆脱我得到的错误,'@request必须是ActionDispatch :: Request',但响应页是空的。这个例子不正确或过时,因为它在教程中? – 2011-02-07 13:34:00

1

一点我已经意识到这个斗争后是不是所有的ID(至少在Rails 3.0.3中),而是一个名为id_error_explanation的类。

固定带将最后一点:

response.should have_selector('div.id_error_explanation')