Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
Created March 24, 2009 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnunemaker/84320 to your computer and use it in GitHub Desktop.
Save jnunemaker/84320 to your computer and use it in GitHub Desktop.
Rails test_helper with jnunemaker's matchy and a few handy custom matchers
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
gem 'jnunemaker-matchy', '>= 0.4.0'
require 'matchy'
class ActiveSupport::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
fixtures :all
custom_matcher :be_nil do |receiver, matcher, args|
matcher.positive_failure_message = "Expected #{receiver} to be nil but it wasn't"
matcher.negative_failure_message = "Expected #{receiver} not to be nil but it was"
receiver.nil?
end
custom_matcher :be_valid do |receiver, matcher, args|
matcher.positive_failure_message = "Expected to be valid but it was invalid #{receiver.errors.inspect}"
matcher.negative_failure_message = "Expected to be invalid but it was valid #{receiver.errors.inspect}"
receiver.valid?
end
custom_matcher :have_error_on do |receiver, matcher, args|
receiver.valid?
attribute = args[0]
expected_message = args[1]
if expected_message.nil?
matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
!receiver.errors.on(attribute).blank?
else
actual = receiver.errors.on(attribute)
matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
actual == expected_message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment