【发布时间】:2012-09-26 00:25:07
【问题描述】:
在我的 Rails 应用程序中,我需要一种非常快速的方法来阻止包含特定查询字符串值的请求。我能够在使用高级约束的路线中做到这一点。它在现实世界中效果很好,但我不知道如何为它编写测试。
这是我的 routes.rb 中的代码:
class BlockedConstraint
@@block_ids = []
@@block_ids << '12345'
@@block_ids << '67890'
def self.matches?(request)
@@block_ids.include?(request.query_parameters["app_id"])
end
end
namespace :api do
match "*path" => "block#block_request", :constraints => BlockedConstraint
match 'ads' => 'status#get_status', :via => :get
match 'init' => 'init#get_init', :via => :get
#etc.
end
这是我的规格:
describe "BlockController" do
it "blocks app_id 12345" do
{ :get => 'api/status', :app_id => '12345' }.should route_to(
:controller => 'api/block',
:action => 'block_request'
)
end
end
但我收到此错误:
Failure/Error: { :get => 'api/status', :app_id => '12345' }.should route_to(
Test::Unit::AssertionFailedError:
The recognized options <{"action"=>"get_status", "controller"=>"api/status"}> did not match <{"action"=>"block_request", "controller"=>"api/block"}>, difference: <{"action"=>"block_request", "controller"=>"api/block"}>
我也在 routing/routes_spec.rb 中尝试过,使用 describe Routing。结果相同。 (实际上我不确定是否最好将其与路由规范或控制器规范一起使用。)
我正在运行 Rails 3.0.10 和 Rspec 2.11.0。
【问题讨论】:
-
尝试将 app_id 烘焙到路径中的路由规范,例如
{ :get => 'api/status?appid=12345' } -
这给了我同样的错误。 :-(
标签: ruby-on-rails rspec rspec-rails