1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

add a should_respond_with_content_type macro for controller tests to check content type of response

This commit is contained in:
Matt Jankowski 2008-09-12 17:51:01 -04:00
parent 4e46d549d0
commit a7057b0800
4 changed files with 28 additions and 0 deletions

View file

@ -31,6 +31,10 @@ module ThoughtBot # :nodoc:
variables_added
}.map(&:to_s)
def response_content_type
@response.headers['Content-Type'] || @response.headers['type']
end
def instantiate_variables_from_assigns(*names, &blk)
old = {}
names = (@response.template.assigns.keys - SPECIAL_INSTANCE_VARIABLES) if names.empty?

View file

@ -134,6 +134,16 @@ module ThoughtBot # :nodoc:
end
end
# Macro that creates a test asserting that the response content type was 'content_type'.
# Example:
#
# should_respond_with_content_type 'application/rss+xml'
def should_respond_with_content_type(content_type)
should "respond with content type of #{content_type}" do
assert_match content_type, response_content_type, "Expected #{content_type} but was actually #{response_content_type}"
end
end
# Macro that creates a test asserting that the controller rendered the given template.
# Example:
#

View file

@ -48,6 +48,16 @@ class PostsControllerTest < Test::Unit::TestCase
should_assign_to :user, :posts
should_not_assign_to :foo, :bar
end
context "viewing posts for a user with rss format" do
setup do
get :index, :user_id => users(:first), :format => 'rss'
end
should_respond_with :success
should_respond_with_content_type 'application/rss+xml'
should_assign_to :user, :posts
should_not_assign_to :foo, :bar
end
end
end

View file

@ -8,6 +8,10 @@ class PostsController < ApplicationController
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @posts.to_xml }
format.rss do
headers['Content-Type'] = 'application/rss+xml'
head :ok
end
end
end