Module: RSpec::Expectations
- Defined in:
- lib/rspec/expectations.rb,
lib/rspec/expectations/differ.rb,
lib/rspec/expectations/errors.rb,
lib/rspec/expectations/handler.rb,
lib/rspec/expectations/version.rb,
lib/rspec/expectations/fail_with.rb,
lib/rspec/expectations/extensions/object.rb
Overview
RSpec::Expectations lets you set expectations on your objects.
result.should == 37
team.should have(11).players_on_the_field
How Expectations work.
RSpec::Expectations adds two methods to Object:
should(matcher=nil)
should_not(matcher=nil)
Both methods take an optional Expression Matcher (See RSpec::Matchers).
When should receives an Expression Matcher, it calls matches?(self). If it returns true, the spec passes and execution continues. If it returns false, then the spec fails with the message returned by matcher.failure_message.
Similarly, when should_not receives a matcher, it calls matches?(self). If it returns false, the spec passes and execution continues. If it returns true, then the spec fails with the message returned by matcher.negative_failure_message.
RSpec ships with a standard set of useful matchers, and writing your own matchers is quite simple. See RSpec::Matchers for details.
Defined Under Namespace
Modules: DeprecatedConstants, Version Classes: Differ, ExpectationNotMetError, InvalidMatcherError, NegativeExpectationHandler, PositiveExpectationHandler
Class Method Summary collapse
- .differ ⇒ Object
-
.fail_with(message, expected = nil, actual = nil) ⇒ Object
Raises an RSpec::Expectations::ExpectationNotMetError with message.
Instance Method Summary collapse
-
#differ=(ignore) ⇒ Object
deprecated
Deprecated.
(no replacement)
Class Method Details
.differ ⇒ Object
5 6 7 |
# File 'lib/rspec/expectations/fail_with.rb', line 5 def differ @differ ||= Differ.new end |
.fail_with(message, expected = nil, actual = nil) ⇒ Object
Raises an RSpec::Expectations::ExpectationNotMetError with message. Adds a diff to the failure message when expected and actual are both present.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rspec/expectations/fail_with.rb', line 16 def fail_with(, expected=nil, actual=nil) if ! raise ArgumentError, "Failure message is nil. Does your matcher define the " + "appropriate failure_message_for_* method to return a string?" end if actual && expected if all_strings?(actual, expected) if any_multiline_strings?(actual, expected) << "\nDiff:" << self.differ.diff_as_string(actual, expected) end elsif no_procs?(actual, expected) && no_numbers?(actual, expected) << "\nDiff:" << self.differ.diff_as_object(actual, expected) end end raise(RSpec::Expectations::ExpectationNotMetError.new()) end |
Instance Method Details
#differ=(ignore) ⇒ Object
(no replacement)
33 34 35 |
# File 'lib/rspec/expectations/extensions/object.rb', line 33 def differ=(ignore) RSpec.deprecate("RSpec::Expectations.differ=(differ)", "nothing at all (diffing is now automatic and no longer configurable)") end |