Question testing rails post

末鹿安然 提交于 2020-01-24 00:51:09

问题


Using Rails 3.0.3.

I have the following route in routes.rb:


 match "user/create_new_password/:reset_password_key" =>"users#create_new_password", :via=>[:get, :post], :as=>:create_new_password

When using this route in the view, with a form, it works ok, however I'm not able to test it. I'm doing this in my functional test:


test "fail create password with invalid key" do
   post :create_new_password, {:create_new_password=>{:password=>"1", :password_confirmation=>"1"}, :reset_password_key=>"user.reset_password_key"} 
end

And I'm getting the error:


ActionController::RoutingError: No route matches {:create_new_password=>{:password=>"1", :password_confirmation=>"1"}, :reset_password_key=>"user.reset_password_key", :controller=>"users", :action=>"create_new_password"}

What's wrong here?


回答1:


So, the problem was in the parameter value for

:reset_password_key

test "fail create password with invalid key" do
   post :create_new_password, {:create_new_password=>{:password=>"1", :password_confirmation=>"1"}, :reset_password_key=>"user.reset_password_key"} 
end

It seems that it's something wrong with the . (dot) in the parameter value.

If I change to other value without the "." (dot), everything is fine. The following works as expected:


test "fail create password with invalid key" do
   post :create_new_password, {:create_new_password=>{:password=>"1", :password_confirmation=>"1"}, :reset_password_key=>"user_reset_password_key"} 
end



来源:https://stackoverflow.com/questions/4948205/question-testing-rails-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!