如何在 Rails 2.3 功能测试中将额外的参数传递给'get'



Fellow Overflowers,

我正在处理一个特殊的问题,找不到明显的解决方案:

我在网址的末尾传递一个参数:

http://localhost:3000/admin/patients/30/verify?unify=true

在我的路由中没有定义参数,定义如下:

verify_admin_patient GET /admin/patients/:id/verify(.:format) {:controller=>"admin/patients", :action=>"verify"}

控制器的作用是读取params[:unify],如果属实,它会验证患者的详细信息,并将其添加到具有相同名称的患者下。如果是false则会创建一个新患者并添加详细信息。

然而,当涉及到测试时,失败是悲惨和令人沮丧的:

test "should verify patient with unification" do
  get :verify, :id => patients(:one).to_param, :unify => "true"
  assert_equal "Successfully created patient.", @response.flash[:notice]
  assert_response :redirect
end

原因是测试实际上并没有将unify参数传递给控制器,因此失败。

我已经尝试了不同的方法来发送参数,例如get '/admin/patients/30/verify?unify=true'或向请求添加更多信息,例如get '/admin/patients/30/verify?unify=true',:action => "verify", :controller => "admin/patients"绝对没有白费。

有没有人知道如何在测试中传递这种参数?

提前谢谢。

 get :verify, :id => patients(:one).to_param, :unify => "true"

 get :verify, :id => patients(:one).to_param, :unify => true

编辑:

前往您的控制器方法,使用

https://github.com/pry/pry

并将其作为方法代码的第一行。运行测试,它应该暂停在那里,表明您确实已到达运行代码的点。

然后逐行运行方法中的代码,假设您没有错误并且记录正在按预期创建,那么问题出在您的测试编写上

Afaik 您必须在测试的第二个参数中传递参数哈希的所有参数,例如:

test "should verify patient with unification" do
  get :verify, { :id => patients(:one).to_param, :unify => true }
  assert_equal "Successfully created patient.", @response.flash[:notice]
  assert_response :redirect
end

这行得通吗?

最新更新