Module: Spec::Expectations
- Included in:
- RspecWorld
- Defined in:
- lib/vendor/plugins/rspec/lib/spec/expectations.rb,
lib/vendor/plugins/rspec/spec/support/spec_classes.rb,
lib/vendor/plugins/rspec/lib/spec/expectations/errors.rb,
lib/vendor/plugins/rspec/lib/spec/expectations/handler.rb,
lib/vendor/plugins/rspec/lib/spec/expectations/fail_with.rb,
lib/vendor/plugins/rspec/lib/spec/runner/differs/default.rb,
lib/vendor/plugins/rspec/spec/spec/expectations/handler_spec.rb
Overview
Spec::Expectations lets you set expectations on your objects.
result.should == 37
team.should have(11).players_on_the_field
How Expectations work.
Spec::Expectations adds two methods to Object:
should(matcher=nil)
should_not(matcher=nil)
Both methods take an optional Expression Matcher (See Spec::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 Spec::Matchers for details.
Defined Under Namespace
Modules: Differs, Helper Classes: ClassWithMultiWordPredicate, ExpectationNotMetError, InvalidMatcherError, NegativeExpectationHandler, Person, PositiveExpectationHandler
Class Attribute Summary collapse
-
.differ ⇒ Object
Returns the value of attribute differ.
Class Method Summary collapse
-
.fail_with(message, expected = nil, target = nil) ⇒ Object
raises a Spec::Expectations::ExpectationNotMetError with message.
Class Attribute Details
.differ ⇒ Object
Returns the value of attribute differ.
4 5 6 |
# File 'lib/vendor/plugins/rspec/lib/spec/expectations/fail_with.rb', line 4 def differ @differ end |
Class Method Details
.fail_with(message, expected = nil, target = nil) ⇒ Object
raises a Spec::Expectations::ExpectationNotMetError with message
When a differ has been assigned and fail_with is passed expected
and target
, passes them to the differ to append a diff message to the failure message.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vendor/plugins/rspec/lib/spec/expectations/fail_with.rb', line 11 def fail_with(, expected=nil, target=nil) # :nodoc: if .nil? raise ArgumentError, "Failure message is nil. Does your matcher define the " + "appropriate failure_message_for_* method to return a string?" end if (Array === ) & (.length == 3) ::Spec.warn(<<-NOTICE ***************************************************************** DEPRECATION WARNING: you are using deprecated behaviour that will be removed from a future version of RSpec. * Support for matchers that return arrays from failure message methods is deprecated. * Instead, the matcher should return a string, and expose methods for the expected() and actual() values. ***************************************************************** NOTICE ) , expected, target = [0], [1], [2] end unless (differ.nil? || expected.nil? || target.nil?) if expected.is_a?(String) << "\n\n Diff:" << self.differ.diff_as_string(target.to_s, expected) elsif expected.is_a?(Hash) && target.is_a?(Hash) << "\n\n Diff:" << self.differ.diff_as_hash(target, expected) elsif !target.is_a?(Proc) << "\n\n Diff:" << self.differ.diff_as_object(target, expected) end end Kernel::raise(Spec::Expectations::ExpectationNotMetError.new()) end |