Browse files

Added restful post request tests. Had to stringify param values for s…

…ome reason
  • Loading branch information...
1 parent 8231c6d commit 3b8c23d54f1102fdfa0d8655320784f0639b5d62 @gus gus committed Jun 2, 2010
View
1 lib/riot/action_controller/http_methods.rb
@@ -12,6 +12,7 @@ def delete(uri, params={}); raise Exception, "DELETE isn't ready yet"; end
private
def perform_request(request_method, uri, params)
http_reset
+ params = params.inject({}) { |acc,(key,val)| acc[key] = val.to_s; acc }

Hey buddy! It seems like this line prevents you from using nested params in your tests. i.e. the following will fail:

 put "/admin/sites/#{a_site.identifier}", { :site => { :name => 'Hey Gabba Gabba' } }

because params will be squashed to:

 { :site => "name" }

What's the deal with this line, anyway? I wasn't sure why it existed.

Cheers,
Dan.

@gus
Thumble Monks member
gus added a note Jul 18, 2010

Seems like I was trying to to_s all the values which must have been necessary for something. Boogers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@env = ::Rack::MockRequest.env_for(uri, {:params => params, :method => request_method})
@env['action_dispatch.show_exceptions'] = false
@app.call(@env)
View
25 test/action_controller/restful_post_request_test.rb
@@ -0,0 +1,25 @@
+require 'teststrap'
+
+context "RESTful POST request on resource" do
+
+ context GremlinsController do
+ setup { post("/gremlins", :id => 2) }
+
+ asserts("request method") { request.request_method }.equals(:post)
+ asserts("controller name") { controller.controller_name }.equals("gremlins")
+ asserts("action name") { controller.action_name }.equals("create")
+ asserts("id param") { controller.params["id"] }.equals("2")
+ asserts("response body") { response.body }.equals("makin' money")
+ end # on a top level resource
+
+ context PartiesController do
+ setup { post("/gremlins/2/parties", "id" => 3) }
+
+ asserts("request method") { request.request_method }.equals(:post)
+ asserts("controller name") { controller.controller_name }.equals("parties")
+ asserts("action name") { controller.action_name }.equals("create")
+ asserts("gremlin_id param") { controller.params["gremlin_id"] }.equals("2")
+ asserts("id param") { controller.params["id"] }.equals("3")
+ asserts("response body") { response.body }.equals("give this monkey what he wants")
+ end # on a nested resource
+end # RESTful POST request
View
4 test/rails_root/app/controllers/gremlins_controller.rb
@@ -6,4 +6,8 @@ def index
def show
render :text => "show me the money"
end
+
+ def create
+ render :text => "makin' money"
+ end
end
View
5 test/rails_root/app/controllers/parties_controller.rb
@@ -2,4 +2,9 @@ class PartiesController < ActionController::Base
def show
render :text => "woot"
end
+
+ def create
+ puts params.inspect
+ render :text => "give this monkey what he wants"
+ end
end

0 comments on commit 3b8c23d

Please sign in to comment.