1
我正在使用rspec,并且当前有测试路由的任务。 在路线我:rspec:带约束条件的测试路由
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"])
}
和测试:
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
测试不及格为一些奇怪的原因。 输出 - No route matches "/qweqwe"
。
虽然用手测试,所有代码工作完美,但我想建立测试它..
你运行'耙路线',以确保路线匹配吗?我并不完全熟悉你在做什么,但IMO可能不应该有一个匹配'/ qweqwe'的路线。看起来像一条奇怪的路线。 – mattforni 2014-11-14 18:39:57
你可以尝试将'public'范围重命名为其他东西吗?这是一个保留字。 – zetetic 2014-11-14 23:56:00
@mattforni它完成允许像site.com/article_name这样的短网址。它完美的工作现场,我只有rspec测试问题。 – 2014-11-15 00:48:10