Skip to content

Instantly share code, notes, and snippets.

@tourdedave
Created May 27, 2014 23:28
Show Gist options
  • Save tourdedave/cd4ef3c53be4715e57b9 to your computer and use it in GitHub Desktop.
Save tourdedave/cd4ef3c53be4715e57b9 to your computer and use it in GitHub Desktop.
def get_job_message
$job_message ? "\n " + $job_message : ''
end
def create_matcher_for(match_symbol, expected_str = nil, not_expected_str = nil, &block)
RSpec::Matchers.define match_symbol do |expected|
match do |actual|
case match_symbol
when :be_true
!!actual
when :be_false
!actual
when :be_nil
actual.nil?
when :eq
actual == expected
when :be_a, :be_an
actual.is_a? expected
when :be_displayed
actual.displayed?
when :include
actual.include? expected
end
end
failure_message_for_should do |actual|
actual_str = nil
expected_str, actual_str = block.call(actual, expected).map(&:to_s) unless block.nil?
"expected: #{expected_str || expected.to_s}\n got: #{actual_str || actual.inspect}#{get_job_message}"
end
failure_message_for_should_not do |actual|
actual_str = nil
expected_str, actual_str = block.call(actual, expected).map(&:to_s) unless block.nil?
"expected: #{not_expected_str || 'not ' + (expected_str || expected.to_s)}\n got: #{actual_str || actual.inspect}#{get_job_message}"
end
end
end
def load_custom_matchers
create_matcher_for :be_displayed, 'displayed'
create_matcher_for :be_false, 'false', 'true'
create_matcher_for :be_nil, 'nil'
create_matcher_for :be_true, 'true', 'false'
create_matcher_for :be_a do |actual, expected|
["a #{expected}", actual.class]
end
create_matcher_for :be_an do |actual, expected|
["an #{expected}", actual.class]
end
create_matcher_for :eq
create_matcher_for :include do |actual, expected|
["include '#{expected}'", actual]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment