Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added block evaluation support to asserts_json_response

  • Loading branch information...
commit 905d31f94ceb6db00d1a475a7be5bebf4ef56714 1 parent b6a4087
@gus gus authored
Showing with 25 additions and 16 deletions.
  1. +10 −8 lib/chicago/riot/macros.rb
  2. +15 −8 test/riot_macros_test.rb
View
18 lib/chicago/riot/macros.rb
@@ -25,19 +25,21 @@ def asserts_location(expected_path)
end.matches(expected_path)
end
- def asserts_json_response(*args)
- content_type = args.length > 1 ? args.shift : 'application/json'
- json = args.shift
- asserts_content_type content_type
+ # Usage:
+ # asserts_json_response({"foo" => "bar"})
+ # asserts_json_response('{"foo":"bar"}')
+ # asserts_json_response("text/javascript;charset=utf-8", {"foo" => "bar"})
+ # asserts_json_response { {"foo" => @some_value} }
+ # asserts_json_response("text/javascript;charset=utf-8") { {"foo" => @some_value} }
+ def asserts_json_response(*args, &block)
+ json = block_given? ? instance_eval(&block) : args.pop
json = json.to_json unless json.instance_of?(String)
- json
-
asserts("response body has JSON") do
last_response.body
end.equals(json)
- # Calling situation is kind of yucky, but maybe not. The maybe not is because of how explicit it is
- # to say "situation" (gus)
+
+ asserts_content_type(args.empty? ? 'application/json' : args.shift)
end
# Usage:
View
23 test/riot_macros_test.rb
@@ -23,13 +23,20 @@
asserts_redirected_to('/foo/bar')
end # asserts redirected to
- context "basic json response" do
- setup { get('/basic-json') }
- asserts_json_response({:foo => "bar"})
- end # asserts redirected to
+ context "json response" do
+ context "basic" do
+ setup { get('/basic-json') }
+ asserts_json_response({:foo => "bar"})
+ end # basic
- context "json response with special content-type" do
- setup { get('/json-with-content-type') }
- asserts_json_response("text/javascript;charset=utf-8", {:foo => "bar"})
- end # asserts redirected to
+ context "with special content-type" do
+ setup { get('/json-with-content-type') }
+ asserts_json_response("text/javascript;charset=utf-8", {:foo => "bar"})
+ end # with special content-type
+
+ context "with content expectation provided as block" do
+ setup { get('/json-with-content-type') }
+ asserts_json_response("text/javascript;charset=utf-8") { {:foo => "bar"} }
+ end # with special content-type
+ end # json response
end
Please sign in to comment.
Something went wrong with that request. Please try again.