Created
September 26, 2013 17:07
-
-
Save cupakromer/6717218 to your computer and use it in GitHub Desktop.
Proposed RSpec Rails matcher for response checks
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
class RespondWithMatcher < RSpec::Matchers::BuiltIn::BaseMatcher | |
def initialize(scope, expected, message = nil) | |
@expected = case expected | |
when Symbol, Fixnum | |
expected | |
when String | |
expected.to_sym | |
else | |
raise ArgumentError, "Unexpected type #{expected} must be symbol or Fixnum" | |
end | |
@message = message | |
@scope = scope | |
end | |
# @api private | |
def matches?(*) | |
match_unless_raises ActiveSupport::TestCase::Assertion do | |
@scope.assert_response expected, @message | |
end | |
end | |
# @api private | |
def failure_message_for_should | |
rescued_exception.message | |
end | |
# @api private | |
def failure_message_for_should_not | |
"expected not to respond with #{expected.inspect}, but did" | |
end | |
end | |
# Delegates to `assert_response` | |
# | |
# @example | |
# | |
# expect(response).to have_responded_with("new") | |
def have_responded_with(type, message = nil) | |
RespondWithMatcher.new(self, type, message) | |
end | |
alias_method :respond_with, :have_responded_with |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment