RSpec 用于路由和专门测试 routes.rb [英] RSpec for routes and testing routes.rb specifically
问题描述
我正在尝试为我的服装路线以及不存在的路线编写测试.
I'm trying to write test for my costume routes, along with non existing routes.
对于这里的例子,我有两个问题:
For the examples down here I have two problems:
- 它匹配我的控制器上不存在的动作,所以它并不真正匹配我的控制器是否有这些动作,它只是匹配语法
should_not be_routable
不起作用,哪一种回到上面的问题,它不检查我的控制器或routes.rb
文件到看看这条路线是否应该存在.
- It matches the actions that don't exist on my controller, so it doesn't really match to see if my controller have those actions, it just matches the syntax
- the
should_not be_routable
doesn't work, which kind of goes back to the above problem of it doesn't check against my controller or theroutes.rb
file to see if the route should exist.
这个通过了,一切都很好:
This one passes and is all good:
it "should route to method1" do
{ :get => '/my_controller/method1' }.should route_to(:controller => 'my_controller',
:action => 'method1')
end
这个失败了,所以它甚至不检查我的控制器或路由文件中是否定义了一个方法 zz.
this one fails, so it doesn't even check to see if I have a method zz defined in my controller or routes file.
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"}
所以它显然根本不看我的路由文件...
So it obviously doesn't look at my routes file at all...
这是另一个例子,它不查看我的路由文件,在我的路由中我有 ' resources :client_rescan, :only => [:index] ' 并且当我执行 rake routes
它没有按预期显示删除,但测试不会查看那些:
This is another example of it doesn't look at my routes file, in my routes I have ' resources :client_rescan, :only => [:index] ' and when I do rake routes
it doesn't show delete as expected, but the test doesn't look at those:
it "should not have route to delete" do
{ :delete => '/my_controller/1'}.should_not be_routable
end
我得到的失败如下.看起来它甚至没有看到删除功能:
The Failure I get is the following. Which looks like it doesn't even see the delete function:
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"}
另一个问题是我在测试中发布到索引的路由:
Another problem is my post routes to index in the test:
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 文件的内容
this is content of my routes.rb file
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
推荐答案
问题是这行routes.rb:
The problem is this line of routes.rb:
match '/:controller(/:action(/:id))'
这是一个包罗万象的路由,将匹配例如/abc/xyz 到控制器 abc 和动作 xyz.
This is a catch all route, and will match for example /abc/xyz to controller abc, and action xyz.
最好不要使用捕获所有路由,而是只公开您想要的功能.
It's best really not to use catch all routes, in favour of exposing only the functionality you want.
这篇关于RSpec 用于路由和专门测试 routes.rb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!