0

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

对于此处的示例,我有两个问题:

  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
4

1 回答 1

5

问题是这行routes.rb:

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

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

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

于 2013-01-15T05:51:37.930 回答