Rspec 测试条件路由约束
我正在尝试编写一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在过去的几天里,这让我抓狂,我在同事的帮助下找到了解决方案。
当使用命名路由约束时,例如示例中的 UserRoleConstraint ,我实际上将约束的
matches?
方法存根到需要它的规范中,即:请注意,要求您具有命名约束,这可能不适用于您的情况。
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 thematches?
method of the constraint int he specs that needed it, i.e.:Note that this requires you to have named constraints, which might not apply to your case.
对于协议约束,您可以只指定带有虚拟域的整个 url。请参阅此答案。
For the protocol constraint you can just specify an entire url with dummy domain. See this answer.