Rspec 测试条件路由约束

发布于 2024-12-07 09:40:45 字数 1441 浏览 0 评论 0原文

我正在尝试编写一些 rspec 集成测试来测试我的条件路由是否正确路由,但我遇到了很多问题。

在routes.rb中:

root :to => "error#ie6", :constraints => {:user_agent => /MSIE 6/}
root :to => "protocol_sets#index", :constraints => UserRoleConstraint.new(/doctor/i)
root :to => "refill_requests#create", :constraints => UserRoleConstraint.new(/member/i)
root :to => "refill_requests#create", :constraints => {:subdomain => "demo"}
root :to => "site#index"

在spec/requests/homepage_routing_spec.rb中,

require 'spec_helper'

describe "User Visits Homepage" do
  describe "Routings to homepage" do
    it "routes / to site#index when no session information exists" do
      visit root_path      
    end
  end
end

当我尝试运行测试时,出现以下错误。

Failures:

  1) User Visits Homepage Routings to homepage routes / to site#index when no session information exists
     Failure/Error: visit root_path
     NoMethodError:
       undefined method `match' for nil:NilClass
     # :10:in `synchronize'
     # ./spec/requests/homepage_routings_spec.rb:6:in `block (3 levels) in '

Finished in 0.08088 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/homepage_routings_spec.rb:5 # User Visits Homepage Routings to homepage routes / to site#index when no session information exists

通过谷歌搜索,我猜测 rspec/capybara 处理条件路由的方式可能存在问题。

有没有办法用 rspec 和 capybara 来测试路由的约束?

I'm trying to write some rspec integration tests to test that my conditional routes are routing correctly, but I'm getting a bunch of problems.

In routes.rb:

root :to => "error#ie6", :constraints => {:user_agent => /MSIE 6/}
root :to => "protocol_sets#index", :constraints => UserRoleConstraint.new(/doctor/i)
root :to => "refill_requests#create", :constraints => UserRoleConstraint.new(/member/i)
root :to => "refill_requests#create", :constraints => {:subdomain => "demo"}
root :to => "site#index"

In spec/requests/homepage_routing_spec.rb

require 'spec_helper'

describe "User Visits Homepage" do
  describe "Routings to homepage" do
    it "routes / to site#index when no session information exists" do
      visit root_path      
    end
  end
end

I get the following error when I try to run the test.

Failures:

  1) User Visits Homepage Routings to homepage routes / to site#index when no session information exists
     Failure/Error: visit root_path
     NoMethodError:
       undefined method `match' for nil:NilClass
     # :10:in `synchronize'
     # ./spec/requests/homepage_routings_spec.rb:6:in `block (3 levels) in '

Finished in 0.08088 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/homepage_routings_spec.rb:5 # User Visits Homepage Routings to homepage routes / to site#index when no session information exists

From Googling around I'm guessing there may be a problem with how rspec/capybara handle conditional routes.

Is there anyway to test constraints on routes with rspec and capybara?

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

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

发布评论

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

评论(2

清引 2024-12-14 09:40:45

在过去的几天里,这让我抓狂,我在同事的帮助下找到了解决方案。

当使用命名路由约束时,例如示例中的 UserRoleConstraint ,我实际上将约束的 matches? 方法存根到需要它的规范中,即:

describe 'routing' do
  context 'w/o route constraint' do 
    before do
      allow(UserRoleConstraint).to receive(:matches?).and_return { false }
    end

    # tests without the route constraint
  end
  context 'w/ route constraint' do 
    before do
      allow(UserRoleConstraint).to receive(:matches?).and_return { true }
    end
  end

  # tests with the route constraint
end

请注意,要求您具有命名约束,这可能不适用于您的情况。

As this drove me nuts over the last days, I found the solution with the help of a colleague.

When using named route constraints, like UserRoleConstraint in the example, I resorted to actually stubbing the matches? method of the constraint int he specs that needed it, i.e.:

describe 'routing' do
  context 'w/o route constraint' do 
    before do
      allow(UserRoleConstraint).to receive(:matches?).and_return { false }
    end

    # tests without the route constraint
  end
  context 'w/ route constraint' do 
    before do
      allow(UserRoleConstraint).to receive(:matches?).and_return { true }
    end
  end

  # tests with the route constraint
end

Note that this requires you to have named constraints, which might not apply to your case.

别理我 2024-12-14 09:40:45

对于协议约束,您可以只指定带有虚拟域的整个 url。请参阅此答案

For the protocol constraint you can just specify an entire url with dummy domain. See this answer.

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