Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implementing respond_with matcher

  • Loading branch information...
commit 56ebdc820bd8cb49500d4dc3019fc1b888005c81 1 parent 674163d
@croaky croaky authored jferris committed
View
1  lib/shoulda/controller/matchers.rb
@@ -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:
View
53 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
View
76 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
+
Please sign in to comment.
Something went wrong with that request. Please try again.