Question testing rails post

2019-06-12 04:00发布

问题:

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