Module: Spec::Expectations

Defined in:
lib/gems/rspec-1.1.11/lib/spec/expectations.rb,
lib/gems/rspec-1.1.11/spec/spec/spec_classes.rb,
lib/gems/rspec-1.1.11/lib/spec/expectations/errors.rb,
lib/gems/rspec-1.1.11/lib/spec/expectations/handler.rb,
lib/gems/rspec-1.1.11/spec/spec/matchers/handler_spec.rb,
lib/gems/rspec-1.1.11/lib/spec/expectations/differs/default.rb,
lib/gems/rspec-1.1.11/lib/spec/expectations/extensions/object.rb,
lib/gems/rspec-1.1.11/lib/spec/expectations/extensions/string_and_symbol.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, ObjectExpectations, StringHelpers Classes: ClassWithMultiWordPredicate, ExpectationMatcherHandler, ExpectationNotMetError, InvalidMatcherError, NegativeExpectationMatcherHandler, Person

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.differObject

Returns the value of attribute differ.



34
35
36
# File 'lib/gems/rspec-1.1.11/lib/spec/expectations.rb', line 34

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.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gems/rspec-1.1.11/lib/spec/expectations.rb', line 41

def fail_with(message, expected=nil, target=nil) # :nodoc:
  if Array === message && message.length == 3
    message, expected, target = message[0], message[1], message[2]
  end
  unless (differ.nil? || expected.nil? || target.nil?)
    if expected.is_a?(String)
      message << "\nDiff:" << self.differ.diff_as_string(target.to_s, expected)
    elsif !target.is_a?(Proc)
      message << "\nDiff:" << self.differ.diff_as_object(target, expected)
    end
  end
  Kernel::raise(Spec::Expectations::ExpectationNotMetError.new(message))
end