RSpec::Expectations adds two instance methods to every object:
should(matcher=nil) should_not(matcher=nil)
Both methods take an optional matcher object (See [RSpec::Matchers](../RSpec/Matchers)). When `should` is invoked with a matcher, it turns around and calls `matcher.matches?(self)`. For example, in the expression:
order.total.should eq(Money.new(5.55, :USD))
the `should` method invokes the equivalent of `eq.matches?(order.total)`. If `matches?` returns true, the expectation is met and execution continues. If `false`, then the spec fails with the message returned by `eq.failure_message_for_should`.
Given the expression:
order.entries.should_not include(entry)
the `should_not` method invokes the equivalent of `include.matches?(order.entries)`, but it interprets `false` as success, and `true` as a failure, using the message generated by `eq.failure_message_for_should_not`.
rspec-expectations ships with a standard set of useful matchers, and writing your own matchers is quite simple.
See [RSpec::Matchers](../RSpec/Matchers) for more information about the built-in matchers that ship with rspec-expectations, and how to write your own custom matchers.
@private
# File lib/rspec/expectations/fail_with.rb, line 5 5: def differ 6: @differ ||= Differ.new 7: end
Raises an RSpec::Expectations::ExpectationNotMetError with message. @param [String] message @param [Object] expected @param [Object] actual
Adds a diff to the failure message when `expected` and `actual` are both present.
# File lib/rspec/expectations/fail_with.rb, line 16 16: def fail_with(message, expected=nil, actual=nil) 17: if !message 18: raise ArgumentError, "Failure message is nil. Does your matcher define the " + 19: "appropriate failure_message_for_* method to return a string?" 20: end 21: 22: if actual && expected 23: if all_strings?(actual, expected) 24: if any_multiline_strings?(actual, expected) 25: expected = expected.join(',') if Array === expected 26: message << "\nDiff:" << differ.diff_as_string(actual, expected) 27: end 28: elsif no_procs?(actual, expected) && no_numbers?(actual, expected) 29: message << "\nDiff:" << differ.diff_as_object(actual, expected) 30: end 31: end 32: 33: raise(RSpec::Expectations::ExpectationNotMetError.new(message)) 34: end
# File lib/rspec/expectations/fail_with.rb, line 42 42: def all_strings?(*args) 43: args.flatten.all? {|a| String === a} 44: end
# File lib/rspec/expectations/fail_with.rb, line 46 46: def any_multiline_strings?(*args) 47: all_strings?(*args) && args.any? {|a| a =~ /\n/} 48: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.