Class: RSpec::Expectations::WhenExpectationTarget

Inherits:
ExpectationTarget
  • Object
show all
Defined in:
lib/rspec/expectations/when_expectation_target.rb

Overview

Note:

WhenExpectationTarget is not intended to be instantiated directly. Use ‘expect(target).when(flag)` instead

Wraps the target of an expectation and allowing of optional expectations as well as testing expectation inverses

Examples:

expect(something).when(flag) # => WhenExpectationTarget testing when `flag`

# `when` used with `to`
expect(actual).when(flag).to eq(3)
# is the equivalent of
if flag
  expect(actual).to eq(3)
end

# `when` used with `with_inverse` and `to`
expect(actual).when(flag).with_inverse.to eq(3)
# is the equivalent of
if flag
  expect(actual).to eq(3)
else
  expect(actual).not_to eq(3)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, when_expected) ⇒ WhenExpectationTarget

Returns a new instance of WhenExpectationTarget.



35
36
37
38
39
# File 'lib/rspec/expectations/when_expectation_target.rb', line 35

def initialize(value, when_expected)
  @target = value
  @when_expected = when_expected
  @with_inverse = false
end

Instance Attribute Details

#targetObject (readonly)

Returns the target of the expectation.

Returns:

  • (Object)

    the target of the expectation



30
31
32
# File 'lib/rspec/expectations/when_expectation_target.rb', line 30

def target
  @target
end

#when_expectedBoolean (readonly)

Returns is the target expected?.

Returns:

  • (Boolean)

    is the target expected?



33
34
35
# File 'lib/rspec/expectations/when_expectation_target.rb', line 33

def when_expected
  @when_expected
end

Instance Method Details

#expect_not_toObject



49
# File 'lib/rspec/expectations/when_expectation_target.rb', line 49

alias_method :expect_not_to, :not_to

#expect_toObject



48
# File 'lib/rspec/expectations/when_expectation_target.rb', line 48

alias_method :expect_to, :to

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

When expected, it will run the given expectation, passing if matcher returns false. When not expected and we’re also testing the inverse, it will run the given expectation, passing if matcher returns true.

Examples:

expect(value).when(flag).not_to eq(5)
expect(value).when(flag).with_inverse.not_to eq(5)

Parameters:

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

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    false if the negative expectation succeeds (else raises)

See Also:

  • Matchers


82
83
84
85
86
87
88
# File 'lib/rspec/expectations/when_expectation_target.rb', line 82

def not_to(matcher = nil, message = nil, &block)
  if @when_expected
    super
  elsif @with_inverse
    expect_to matcher, message, &block
  end
end

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

When expected, it will run the given expectation, passing if matcher returns true. When not expected and we’re also testing the inverse, it will run the given expectation, passing if matcher returns false.

Examples:

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

Parameters:

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

    optional message to display when the expectation fails

Returns:

  • (Boolean)

    true if the expectation succeeds (else raises)

See Also:

  • Matchers


63
64
65
66
67
68
69
# File 'lib/rspec/expectations/when_expectation_target.rb', line 63

def to(matcher = nil, message = nil, &block)
  if @when_expected
    super
  elsif @with_inverse
    expect_not_to matcher, message, &block
  end
end

#with_inverseObject

Indicates whether the inverse of the expectation should be applied should the when flag be false



43
44
45
46
# File 'lib/rspec/expectations/when_expectation_target.rb', line 43

def with_inverse
  @with_inverse = true
  self
end