Skip to content

Commit

Permalink
implementing respond_with matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Croak authored and jferris committed Feb 7, 2009
1 parent 674163d commit 56ebdc8
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/shoulda/controller/matchers.rb
Expand Up @@ -2,6 +2,7 @@
require 'shoulda/controller/matchers/filter_param_matcher'
require 'shoulda/controller/matchers/set_the_flash_matcher'
require 'shoulda/controller/matchers/render_with_layout_matcher'
require 'shoulda/controller/matchers/respond_with_matcher'

module Shoulda # :nodoc:
module Controller # :nodoc:
Expand Down
53 changes: 53 additions & 0 deletions lib/shoulda/controller/matchers/respond_with_matcher.rb
@@ -0,0 +1,53 @@
module Shoulda # :nodoc:
module Controller # :nodoc:
module Matchers

# docs
def respond_with(status)
RespondWithMatcher.new(status)
end

class RespondWithMatcher # :nodoc:

def initialize(status)
@status = symbol_to_status_code(status)
end

def matches?(controller)
@controller = controller
correct_status_code? || correct_status_code_range?
end

def failure_message
"Expected status to be #{@status}"
end

protected

def correct_status_code?
@controller.response.response_code == @status
end

def correct_status_code_range?
@status.is_a?(Range) &&
@status.include?(@controller.response.response_code)
end

def symbol_to_status_code(potential_symbol)
case potential_symbol
when :success then 200
when :redirect then 300..399
when :missing then 404
when :error then 500..599
when Symbol
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[potential_symbol]
else
potential_symbol
end
end

end

end
end
end
76 changes: 76 additions & 0 deletions test/matchers/controller/respond_with_matcher_test.rb
@@ -0,0 +1,76 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')

class RespondWithMatcherTest < Test::Unit::TestCase # :nodoc:

context "a controller responding with success" do
setup do
@controller = build_response { render :text => "text", :status => 200 }
end

should "accept responding with 200" do
assert_accepts respond_with(200), @controller
end

should "accept responding with :success" do
assert_accepts respond_with(:success), @controller
end
end

context "a controller responding with redirect" do
setup do
@controller = build_response { render :text => "text", :status => 301 }
end

should "accept responding with 301" do
assert_accepts respond_with(301), @controller
end

should "accept responding with :redirect" do
assert_accepts respond_with(:redirect), @controller
end
end

context "a controller responding with missing" do
setup do
@controller = build_response { render :text => "text", :status => 404 }
end

should "accept responding with 404" do
assert_accepts respond_with(404), @controller
end

should "accept responding with :missing" do
assert_accepts respond_with(:missing), @controller
end
end

context "a controller responding with error" do
setup do
@controller = build_response { render :text => "text", :status => 500 }
end

should "accept responding with 500" do
assert_accepts respond_with(500), @controller
end

should "accept responding with :error" do
assert_accepts respond_with(:error), @controller
end
end

context "a controller responding with not implemented" do
setup do
@controller = build_response { render :text => "text", :status => 501 }
end

should "accept responding with 501" do
assert_accepts respond_with(501), @controller
end

should "accept responding with :not_implemented" do
assert_accepts respond_with(:not_implemented), @controller
end
end

end

0 comments on commit 56ebdc8

Please sign in to comment.