Class: RSpecial::Expect

Inherits:
Object
  • Object
show all
Defined in:
lib/rspecial/expect.rb

Overview

Wraps the target of an expectation.

Examples:

expect(something) # => Expect

# used with `to`
expect(actual).to eq(3)

# with `to_not`
expect(actual).to_not eq(3)

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Expect

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Expect.



17
18
19
# File 'lib/rspecial/expect.rb', line 17

def initialize(target)
  @target = target
end

Instance Method Details

#to(matcher = nil, message = nil, &block) ⇒ Boolean

Runs the given expectation, passing if ‘matcher` returns true.

Examples:

expect(value).to eq(5)
expect { perform }.to raise_error

Parameters:

  • matcher (Matcher) (defaults to: nil)
  • message (String) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    true if the expectation succeeds (else raises)

See Also:

  • RSpec::Matchers


36
37
38
39
# File 'lib/rspecial/expect.rb', line 36

def to(matcher=nil, message=nil, &block)
  #prevent_operator_matchers(:to, matcher)
  handle_positive_matcher(@target, matcher, message, &block)
end

#to_not(matcher = nil, message = nil, &block) ⇒ Boolean Also known as: not_to

Runs the given expectation, passing if ‘matcher` returns false.

Examples:

expect(value).to_not eq(5)
expect(value).not_to eq(5)

Parameters:

  • matcher (Matcher) (defaults to: nil)
  • message (String) (defaults to: nil)

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    false if the negative expectation succeeds (else raises)

See Also:

  • RSpec::Matchers


56
57
58
59
# File 'lib/rspecial/expect.rb', line 56

def to_not(matcher=nil, message=nil, &block)
  #prevent_operator_matchers(:to_not, matcher)
  handle_negative_matcher(@target, matcher, message, &block)
end