[rspec-users] [Cucumber, Rails] Testing arbitrary post action parameters

James Byrne lists at ruby-forum.com
Sat Jan 17 21:47:31 EST 2009


Matt Wynne wrote:

> 
> If you look at the Rails documentation for the named route methods, I
> think you might want to try something a bit more like this:
> 
>      user = User.find_by_username!(name)
>      put users_url(user, :user => {:administrator => true})
> 

Funnily enough, I tried this exact thing before I ended up with

 put users_url( :user => User.find_by_username!(name) ), :user =>
{:administrator => true}


When I used the form that Pat provided
  put users_url(user), :user => {:administrator => true}
then I saw this:

    And the user named "myuser" enables the administrator role       # 
features/
app/models/users/step_definitions/user_steps.rb:28
      You have a nil object when you didn't expect it!
      The error occurred while evaluating nil.to_sym (NoMethodError)
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/routing
/route.rb:243:in `extra_keys'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/routing
/route.rb:243:in `map'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/routing
/route.rb:243:in `extra_keys'
      generated code 
(/usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_co
ntroller/routing/route.rb:154):3:in `generate'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/routing
/route_set.rb:339:in `generate'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/url_rew
riter.rb:208:in `rewrite_path'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/url_rew
riter.rb:187:in `rewrite_url'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/url_rew
riter.rb:165:in `rewrite'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/base.rb
:626:in `url_for'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/integra
tion.rb:227:in `url_for'
      (eval):18:in `users_url'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/integra
tion.rb:498:in `__send__'
      /usr/lib/ruby/gems/1.8/gems/actionpack-2.2.2/lib/action_controller/integra
tion.rb:498:in `method_missing'
      ./features/app/models/users/step_definitions/user_steps.rb:30:in 
`And /use
r named "(.*)" enables the administrator role/'
      features/app/models/users/user.feature:44:in `And the user named 
"myuser"
enables the administrator role'

If I removed the enclosing parenthesis around my_user then I saw the 
same thing.


> If you think you know the URL you want, why not try hard-coding it for
> a while in the step until you get the desired effect on the
> controller? Then you can add a spec for your routing which proves that
> the call to users_url() with the parameters you're trying gives you
> the same hard-coded value. Then you'll have discovered for yourself
> exactly the right parameters to use to send the right PUT request all
> the way to the controller.
> 
> Make sense?

Yes, and in rough form this is what I ended up doing.  The step 
definition I ended up with appears to be the correct method signature 
and it is equivalent to the example you provide.  It seems that the 
first parameter to _put_ must be a hash.

Nonetheless, I cannot seem to duplicate the exact effect of an update 
via the /account action (yes, I have repeated the test with put 
account_url(... and obtained the same results as trying put 
users_url(... )




-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list