Ruby on rails 在Rails控制器测试中,是否有方法传递查询(非路由)参数?
我正在用Rails和RSpec编写控制器测试,从阅读Ruby on rails 在Rails控制器测试中,是否有方法传递查询(非路由)参数?,ruby-on-rails,ruby,testing,rspec,Ruby On Rails,Ruby,Testing,Rspec,我正在用Rails和RSpec编写控制器测试,从阅读ActionController::TestCase的源代码可以看出,不可能将任意查询参数传递给控制器——只能传递路由参数 为了克服这个限制,我目前正在使用和_routing: with_routing do |routes| # this nonsense is necessary because # Rails controller testing does not # pass on query params, only ro
ActionController::TestCase
的源代码可以看出,不可能将任意查询参数传递给控制器——只能传递路由参数
为了克服这个限制,我目前正在使用和_routing
:
with_routing do |routes|
# this nonsense is necessary because
# Rails controller testing does not
# pass on query params, only routing params
routes.draw do
get '/users/confirmation/:confirmation_token' => 'user_confirmations#show'
root :to => 'root#index'
end
get :show, 'confirmation_token' => CONFIRMATION_TOKEN
end
正如您可能猜到的,我正在测试Desive的自定义确认控制器。这意味着我正在升级到一个现有的API,并且没有选项来更改config/routes.rb
中实际映射的完成方式
有没有更整洁的方法?get
传递查询参数的支持方式
编辑:还有别的事情。我在中创建了一个最小的示例:
spec/controllers/receive\u query\u param\u controller\u spec.rb
describe ReceiveQueryParamController do
describe '#please' do
it 'receives query param, sets @my_param' do
get :please, :my_param => 'test_value'
assigns(:my_param).should eq 'test_value'
end
end
end
class ReceiveQueryParamController < ApplicationController
def please
@my_param = params[:my_param]
end
end
So13866283::Application.routes.draw do
get '/receive_query_param/please' => 'receive_query_param#please'
end
app/controllers/receive\u query\u param\u controller.rb
describe ReceiveQueryParamController do
describe '#please' do
it 'receives query param, sets @my_param' do
get :please, :my_param => 'test_value'
assigns(:my_param).should eq 'test_value'
end
end
end
class ReceiveQueryParamController < ApplicationController
def please
@my_param = params[:my_param]
end
end
So13866283::Application.routes.draw do
get '/receive_query_param/please' => 'receive_query_param#please'
end
这个测试通过了,所以我想是Desive在路由方面做了一些古怪的事情
编辑: 锁定在设计路线中定义的位置,并更新我的示例应用程序以匹配它
So13866283::Application.routes.draw do
resource :receive_query_param, :only => [:show],
:controller => "receive_query_param"
end
。。。规格和控制器相应更新,以使用
#show
。测试仍然通过,即params[:my_param]
由get:show,:my_param=>“blah”
填充。所以,我的真实应用程序中为什么没有出现这种情况仍然是个谜。你确定没有其他事情发生吗?我有一个Rails 3.0.x项目,正在传递参数。。好。。这是一个帖子。。也许get的情况有所不同,但这似乎很奇怪
before { post :contact_us, :contact_us => {:email => 'joe@example.com',
:category => 'Category', :subject => 'Subject', :message => 'Message'} }
在我的控制器中,
params
对象中肯定使用了上述内容。控制器测试不会路由。您正在对控制器进行单元测试--路由超出了它的范围
典型的控制器规范示例测试一个操作:
describe MyController do
it "is successful" do
get :index
response.status.should == 200
end
end
通过将参数传递到get
,可以设置测试上下文,例如:
get :show, :id => 1
您可以在该散列中传递查询参数
如果您确实想测试路由,可以编写路由规范或请求(集成)规范。我现在正在这样做:
@request.env['QUERY_STRING'] = "confirmation_token=" # otherwise it's ignored
get :show, :confirmation_token => CONFIRMATION_TOKEN
。。。但它看起来很粗糙
如果有人能给我展示一个简洁而正式的方法,我会很高兴的。从我在
#get
的源代码和它调用的所有东西中看到的来看,似乎没有其他方法,但我希望我忽略了一些东西。与之不同——这是关于路由测试的。但是我会看看我是否能从中学到一些东西,如果“extras”也能与get
一起使用。。。如果我只能找到query\u参数的定义位置以及如何填充它,那么链中的下一步是GET
及其别名query\u参数的定义和覆盖Rack::Request
中的GET
。我正在测试控制器,而不是路由。不幸的是,Rails不允许我发送我想要的参数,因为它纠缠着路由和控制器测试。这就是为什么我需要和_routing
。我想我知道这里发生了什么。您已经定义了一个资源,该资源需要在params散列中包含一个id,但您没有在规范中传递它。只要id存在,您就可以在该散列中传递id和任何其他查询参数。即:{:id=>“1”,:hello=>“world”}
否,现有路由不需要:id
,并且没有错误消息get
只是不传递我给它的任何参数,除非这些参数在路由路径中。你确定get:show
不需要:id吗?路由是如何定义的?rake routes CONTROLLER=user\u confirmations
给我GET/users/confirmation(:format)user\u confirmations#show
是的,我认为帖子的处理方式不同。代码只是偏向于get查询参数,“rails方式”显然是将内容作为路径的一部分传递。我会检查Desive是如何测试自己的,也许有一些线索。哦,Desive只对这个控制器进行集成测试,所以这个问题不会出现。