[英]rspec: test routes with constraints
I'm using rspec, and currently have a task to test routing. 我正在使用rspec,目前有一个测试路由的任务。 In routes I have: 在路线中,我有:
get ':url' => 'article#get', constraints: lambda { |request|
Article.public.where(url: request.params["url"]).first
}
get ':url' => 'product#get', constraints: lambda { |request|
Product.get_publick_prod_by_url(request.params["url"])
}
and test: 并测试:
let!(:article) {Article.create(name: "qweqwe",
short_description: "sh",
description: "dscr",
public: true,
meta_title: "title",
meta_description: "meta-descr",
meta_keywords: "qwe",
url: "qweqwe")}
before(:each) do
Epk::Application.reload_routes!
end
it "should route to article" do
expect(get: article.url).to route_to(controller: 'article', action: 'get', url: "qweqwe")
end
test is not passing for some strange reasons. 由于某些奇怪的原因,测试未通过。 Output - No route matches "/qweqwe"
. 输出- No route matches "/qweqwe"
。
While testing by hands, all code works perfectly, but I want to set up test for it.. 手动测试时,所有代码都能完美运行,但是我想为此设置测试。
The problem is in route_to method, that not passing params for some reasons. 问题出在route_to方法中,由于某些原因未传递参数。 In my situation with simple url like "site.com/:url" I can rewrite route's constraints in this way: Article.find_published_by_url(request.path_info.split('/')[1])
, and it works perfectly. 在使用简单网址(例如“ site.com/:url”)的情况下,我可以通过以下方式重写路由约束: Article.find_published_by_url(request.path_info.split('/')[1])
,它可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.