Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created September 26, 2013 17:07
Show Gist options
  • Save cupakromer/6717218 to your computer and use it in GitHub Desktop.
Save cupakromer/6717218 to your computer and use it in GitHub Desktop.
Proposed RSpec Rails matcher for response checks
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