我正在为一个相当大的 Rails 3 应用程序编写我们的路由的 RSpec 测试套件。许多路由使用“MATCH”,但它们都不应该,特别是因为我们必须在过渡到 Rails 4 时重写它们。
我的大部分it
块看起来像这样:
it "with /comments/account/100" do
expect(get("comments/account/100")).to route_to("comments#list", :account_id => "100")
expect(post("comments/account/100")).to route_to("comments#list", :account_id => "100")
expect(put("comments/account/100")).to route_to("comments#list", :account_id => "100")
expect(delete("comments/account/100")).to route_to("comments#list", :account_id => "100")
end
不得不写无数这样的块似乎有点,非 DRY。我想要一个看起来像这样的匹配器:
expect(match_all_verbs("/comments/accounts/100")).to route_to("comments#list", :account_id => "100")
编辑:最终工作版本,感谢史蒂文:
def match_all_verbs(path, method, options = {})
[:get, :post, :put, :delete].each do |verb|
expect(send(verb, path)).to route_to(method, options)
end
end
我添加了一个options
哈希,这样我就可以将参数传递给路由。一切似乎都运行良好。
为了好玩,我做了一个match_no_verbs
测试.to_not be_routable
匹配器组合:
def match_no_verbs(path, method, options = {})
[:get, :post, :put, :delete].each do |verb|
expect(send(verb, path)).to_not route_to(method, options)
end
end
非常感谢!