【问题标题】:RSpec for routes and testing routes.rb specifically用于路由和测试 routes.rb 的 RSpec
【发布时间】:2013-01-15 02:36:02
【问题描述】:

我正在尝试为我的服装路线以及不存在的路线编写测试。

对于下面的示例,我有两个问题:

  1. 它匹配我的控制器上不存在的动作,所以它并不真正匹配我的控制器是否有这些动作,它只是匹配语法
  2. should_not be_routable 不起作用,这又回到了上面的问题,它没有检查我的控制器或routes.rb 文件以查看路由是否应该存在。

这个通过了,一切都好:

it "should route to method1" do
   { :get => '/my_controller/method1' }.should route_to(:controller => 'my_controller',
                                                     :action => 'method1')
end

这个失败了,所以它甚至不检查我的控制器或路由文件中是否定义了方法 zz。

it "should not route to zz" do
  { :get => '/my_controller/zz' }.should_not be_routable 
end

我得到的错误是:

MyController routing zz should route to rescan
     Failure/Error: { :get => '/my_controller/zz' }.should_not be_routable
       expected {:get=>"/client_rescan/zz"} not to be routable, but it routes to {:controller=>"my_controller", :action=>"zz"}

所以它显然根本不看我的路线文件......

这是另一个不查看我的路线文件的示例,在我的路线中我有'资源:client_rescan,:only => [:index]',当我这样做rake routes时,它不显示删除正如预期的那样,但测试不看那些:

it "should not have route to delete" do
  { :delete => '/my_controller/1'}.should_not be_routable
end

我得到的失败如下。看起来它甚至没有看到删除功能:

 Failure/Error: { :delete => '/my_controller/1'}.should_not be_routable
   expected {:delete=>"/my_controller/1"} not to be routable, but it routes to {:controller=>"my_controller", :action=>"1"}

另一个问题是我在测试中的索引发布路线:

it "should not have route to post" do
  { :post => '/my_controller' }.should_not be_routable
end

我得到的失败是:

 Failure/Error: { :post => '/my_controller' }.should_not be_routable
   expected {:post=>"/my_controller"} not to be routable, but it routes to {:controller=>"my_controller", :action=>"index"}

这是我的 routes.rb 文件的内容

require 'resque/server'
require 'resque_scheduler'

Cdc::Application.routes.draw do

  mount Resque::Server.new, :at => 'resque'
  Resque.schedule = YAML.load_file(File.join(File.dirname(__FILE__), 'resque_schedule.yml')) # load the schedule

  devise_for :users, :controllers => { :sessions => "sessions", :registrations => "registrations" }



  [:assessments, :security_assessments, :privacy_assessments].each do |assessment_type|
    resources assessment_type, :controller => :assessments do
      resources :rsz do
        post 'review', :on => :member
        get 'judgement', :on => :member
      end

      get :judgement, :on => :member

      resources :samples do
        get 'download', :on => :member
      end

      resources :asm_reviews do
        post :request_review, :on => :collection
      end
    end
  end



  resources :account_creator
  match "/images/captcha/*file_name" => "captcha#show"

  ## this is where my route is!! :)

  resources :my_controller, :only => [:index] do
    collection do
      get :rescan
      get :cancel
    end
  end

  match "alert_queue/words" => "alert_queue#words"
  resources :alert_queue

  match "calls_to_action/start/:id" => "calls_to_action#start", :id => /\d+/
  match "calls_to_action/close/:id" => "calls_to_action#close", :id => /\d+/
  match "calls_to_action/comment/:id" => "calls_to_action#comment", :id => /\d+/
  match "calls_to_action/multiclose" => "calls_to_action#multiclose"
  match "calls_to_action/multiscan" => "calls_to_action#multiscan"

  match "application_instances/multiclose" => "application_instances#multiclose"
  match "application_instances/multiscan" => "application_instances#multiscan"

  resources :code_categories do
    resources :code_families do
      resources :code_family_versions
    end
  end

  resources :code_policy_items


  resources :application_instances do
    collection do
      post :resque_statuses
    end

    resources :code_indices
    resources :application_analysis

    get "smali/:smali_path", :as => :smali, :on => :member, :action => :smali, :smali_path => /.*/
    member do
      get :file
      get :strings
      get :dump
      get :alerts
      get :correlations
      get :signers
      get :icon
      get :resque_status
      get :assets
      get :new_code_index
      get :class_list
      get :heuristic_hits
      get :engine_artifacts
      post :rescan
      post :attach_artifact
      post :engine_artifact, :action => :attach_engine_artifact
      resources :alerts
    end

    post "multiscan", :on => :collection, :action => :multiscan
    post "multiclose", :on => :collection, :action=> :multiclose
    post "engines/:engine_name/:engine_version/assertions/:cookie",
         :on => :member, :action => :register_assertion_set, :as => :register_assertion_set,
         :engine_name => /[^\/]+/, :engine_version => /[^\/]+/, :cookie => /[^\/]+/
    post "engines/:engine_name/:engine_version/network_data",
         :on => :member, :action => :register_network_data, :as => :register_network_data,
         :engine_name => /[^\/]+/, :engine_version => /[^\/]+/

    post "assets", :on => :member, :action => :register_asset_set, :as => :register_asset_set
  end

  # index gets the list of families, show gets the assertion types for that family
  resources :assertion_families

  resources :artifacts

  resources :dashboard do
    collection do
      get :last_app_instance
    end
  end

  match '/direct_downloads/' => 'direct_downloads#index'

  root :to => "stats#index"
  match '/' => 'stats#index'
  match 'explorer/query/:format' => 'explorer#query'
  match '/:controller(/:action(/:id))'
end

【问题讨论】:

  • 最后一次测试是通过还是失败?如果失败,你得到什么错误?
  • 对不起,我刚刚用错误更新了问题。这么想but it routes to {:controller=>"my_controller", :action=>"1"
  • @Meena 对不起,但您的评论“非常想,但它路由到”没有任何意义。你能重新阅读并更新它吗?
  • @99miles 抱歉,我没有解释太多,因为我将响应作为 qeustion 正文的一部分,因此人们不必阅读 cmets。所以我得到的错误与上面相同,它是:`Failure/Error: { :delete => '/my_controller/1'}.should_not be_routable expected {:delete=>"/my_controller/1"} 不可路由,但它路由到 {:controller=>"my_controller", :action=>"1"}`
  • 您能否发布您的 routes.rb 文件的内容以及 rake 路由的输出?

标签: ruby ruby-on-rails-3 rspec


【解决方案1】:

问题出在这行routes.rb:

match '/:controller(/:action(/:id))'

这是一条包罗万象的路线,例如,将匹配 /abc/xyz 到控制器 abc 和动作 xyz。

最好不要使用 catch all 路由,而是只公开你想要的功能。

【讨论】:

  • 没问题,乐于助人! :)
  • 向每一个因此注定了未来开发者的承包商挥手致意!
猜你喜欢
  • 1970-01-01
  • 2018-10-01
  • 2013-07-13
  • 2011-08-21
  • 2011-08-04
  • 2014-08-02
  • 2011-10-31
  • 2012-02-12
  • 1970-01-01
相关资源
最近更新 更多