我想在我的文章控制器中测试永久链接操作,该控制器使用命名路由(/ permalink / terms-of-use):
map.permalink 'permalink/:permalink',
:controller => :articles, :action => :permalink, :as => :permalink
这是规范:
describe "GET permalink" do
it "should visit an article" do
get "/permalink/@article.permalink"
end
end
但是我收到了这个错误:
'ArticlesController中的ActionController :: RoutingError永久链接呈现页面' 没有路线匹配{:controller =>“articles”,:action =>“/ permalink/@article.permalink”}更新:
任何想法如何写GET?
答案 0 :(得分:3)
错误是因为您将整个URL传递给期望控制器的某个操作方法名称的方法。如果我理解正确,你会尝试一次测试几件事。
测试路由的名称与测试路由不同,与测试控制器操作不同。这是我测试控制器动作的方式(这可能并不奇怪)。请注意,我匹配您的命名,而不是推荐我使用的。
在spec / controllers / articles_controller_spec.rb中,
describe ArticlesController do
describe '#permalink' do
it "renders the page" do
# The action and its parameter are both named permalink
get :permalink :permalink => 666
response.should be_success
# etc.
end
end
end
以下是我只使用rspec-rails测试命名路由的方法:
在spec / routing / articles_routing_spec.rb中,
describe ArticlesController do
describe 'permalink' do
it 'has a named route' do
articles_permalink(666).should == '/permalink/666'
end
it 'is routed to' do
{ :get => '/permalink/666' }.should route_to(
:controller => 'articles', :action => 'permalink', :id => '666')
end
end
end
Shoulda的路由匹配器更简洁,同时仍然提供了一个很好的描述和失败消息:
describe ArticlesController do
describe 'permalink' do
it 'has a named route' do
articles_permalink(666).should == '/permalink/666'
end
it { should route(:get, '/permalink/666').to(
:controller => 'articles', :action => 'permalink', :id => '666' })
end
end
AFAIK RSpec和Shoulda都没有一种特定的,简洁的测试命名路线的方法,但你可以编写自己的匹配器。
答案 1 :(得分:0)
describe "GET permalink" do
it "should visit an article" do
get "/permalink/#{@article.permalink}"
end
end