路由在 RSpec 集成测试中不可用

发布于 2024-09-25 12:29:18 字数 1248 浏览 1 评论 0原文

我正在按照 Rails 教程网站 中的示例进行操作,但在使集成测试正常工作时遇到问题。特别是教程中第 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

这是 github 上的完整测试文件

但是,如果我一次运行所有测试,则不会收到错误。< /strong> 仅当我运行该单独测试时才会发生该错误。

我的项目可以在 github 上查看此处

如何修复此错误?

I am following the examples in the Rails Tutorial website and I'm having issues getting the integration tests to work. Specifically the example at listing 8.20 in section 8.4.2 in the tutorial.

At the visit signup_path line of code below I get the following error: "undefined local variable or method `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

However, if I run all the tests at once, then I do not get the error. The error only happens when I run that individual test.

My project can be viewed on github here

How do I fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-10-02 12:29:18

经过一番努力,我意识到这根本不是一个 ID(至少在 Rails 3.0.3 中),而是一个名为 id_error_explanation 的类。

修复了将最后一位替换为:

response.should have_selector('div.id_error_explanation')

After a bit of a struggle I've realized this isn't an ID at all (at least in Rails 3.0.3), rather a class, named id_error_explanation.

Fixed with replacing the last bit with:

response.should have_selector('div.id_error_explanation').

脸赞 2024-10-02 12:29:18

您应该根据清单 8.21 更改测试。测试将如下所示:

spec/requests/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

You are supposed to change the test according to listing 8.21. The test would then look like this:

spec/requests/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文