rspec说没有路由匹配,但路由在浏览器中工作

时间:2013-02-04 09:27:33

标签: ruby-on-rails

问题很简单,我不知道可能出现什么问题。

我有这个规格:

it "/fr/admin/articles/new" do
  expect(:get => "/fr/admin/articles/new").to route_to(:controller => 'articles', :action => 'new')
end

当我浏览localhost:3000 / fr / admin / articles / new时它有效!

但我的测试说:

Failure/Error: expect(:get => "/fr/admin/articles/new").to route_to(:controller => 'articles', :action => 'new')
   No route matches "/fr/admin/articles/new"
   # ./spec/routing/articles_routing_spec.rb:6:in `block (2 levels) in <top (required)>'

my routes.rb:

scope '/:locale', :constraints => LocaleConstraint do
  root :to => 'static#home'
  scope '/admin' do
    root :to => 'articles#index'
    resources :articles
  end
end

也是我的locale_constraint.rb

class LocaleConstraint
  def self.matches?(request)
    /(fr|zh)/.match(request.path_parameters[:locale])
  end
end

rake路线的输出:

    ckeditor        /ckeditor                                  Ckeditor::Engine
       admin        /admin(.:format)                           :controller#:action
       login GET    /login(.:format)                           sessions#new
      logout GET    /logout(.:format)                          sessions#destroy
    sessions POST   /sessions(.:format)                        sessions#create
        root        /:locale(.:format)                         static#home
        root        /:locale/admin(.:format)                   articles#index
    articles GET    /:locale/admin/articles(.:format)          articles#index
             POST   /:locale/admin/articles(.:format)          articles#create
 new_article GET    /:locale/admin/articles/new(.:format)      articles#new
edit_article GET    /:locale/admin/articles/:id/edit(.:format) articles#edit
     article GET    /:locale/admin/articles/:id(.:format)      articles#show
             PUT    /:locale/admin/articles/:id(.:format)      articles#update
             DELETE /:locale/admin/articles/:id(.:format)      articles#destroy
        root        /                                          :controller#:action

Routes for Ckeditor::Engine:
        pictures GET    /pictures(.:format)             ckeditor/pictures#index
                 POST   /pictures(.:format)             ckeditor/pictures#create
         picture DELETE /pictures/:id(.:format)         ckeditor/pictures#destroy
attachment_files GET    /attachment_files(.:format)     ckeditor/attachment_files#index
                 POST   /attachment_files(.:format)     ckeditor/attachment_files#create
 attachment_file DELETE /attachment_files/:id(.:format) ckeditor/attachment_files#destroy

1 个答案:

答案 0 :(得分:1)

显然,这是Rails中的一个错误,请参阅:

https://github.com/rails/rails/issues/8679