Permalink
Browse files

Test XML and JSON responses

  • Loading branch information...
joshuaclayton committed Dec 21, 2009
1 parent 5007257 commit 3d9c8fb33ff8dd1c3bd704b4f6bd7fdb83403260
Showing with 20 additions and 3 deletions.
  1. +5 −3 app/controllers/peas_controller.rb
  2. +15 −0 test/functional/peas_controller_test.rb
@@ -4,11 +4,13 @@ class PeasController < InheritedResources::Base
def show
show! do |format|
- @short_url = peapod_url(:pea_key => @pea.key)
+ @short_url = peapod_url(@pea.key)
format.json do
render :json => {
- :long_url => @pea.long_url,
- :short_url => @short_url
+ :pea => {
+ :long_url => @pea.long_url,
+ :short_url => @short_url
+ }
}
end
end
@@ -9,17 +9,32 @@ class PeasControllerTest < ActionController::TestCase
context "as HTML" do
setup { get :show, :id => @pea.to_param }
should_assign_to(:pea) { @pea }
+ should_assign_to(:short_url) { peapod_url(@pea.key) }
should_render_template :show
end
context "as JSON" do
setup { get :show, :id => @pea.to_param, :format => "json" }
+
should_respond_with_content_type :json
+
+ should "contain the correct data" do
+ json_response = ActiveSupport::JSON.decode(@response.body)
+ assert_equal @pea.long_url, json_response["pea"]["long_url"]
+ assert_equal peapod_url(@pea.key), json_response["pea"]["short_url"]
+ end
end
context "as XML" do
setup { get :show, :id => @pea.to_param, :format => "xml" }
should_respond_with_content_type :xml
+
+ should "contain the correct data" do
+ assert_select "pea" do
+ assert_select "short_url", peapod_url(@pea.key)
+ assert_select "long_url", @pea.long_url
+ end
+ end
end
end
end

0 comments on commit 3d9c8fb

Please sign in to comment.