forked from technicalpickles/shoulda
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|