RSpec用于路由和测试路由。具体而言

时间:2013-01-15 02:36:03

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

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

对于这里的例子我有两个问题:

  1. 它匹配我的控制器上不存在的操作,因此它与我的控制器是否具有这些操作并不匹配,它只是匹配语法
  2. should_not be_routable不起作用,这种情况可以追溯到上面的问题,它不会检查我的控制器或routes.rb文件以查看路由是否应该存在。
  3. 这个通过并且一切都很好:

    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
    

1 个答案:

答案 0 :(得分:5)

问题在于这行routes.rb:

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

这是一个捕获所有路由,并且将匹配例如/ abc / xyz到控制器abc和动作xyz。

最好不要使用catch所有路由,而只是暴露你想要的功能。