Class: RSpec::Expectations::WhenExpectationTarget
- Inherits:
-
ExpectationTarget
- Object
- ExpectationTarget
- RSpec::Expectations::WhenExpectationTarget
- Defined in:
- lib/rspec/expectations/when_expectation_target.rb
Overview
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
Instance Attribute Summary collapse
-
#target ⇒ Object
readonly
The target of the expectation.
-
#when_expected ⇒ Boolean
readonly
Is the target expected?.
Instance Method Summary collapse
- #expect_not_to ⇒ Object
- #expect_to ⇒ Object
-
#initialize(value, when_expected) ⇒ WhenExpectationTarget
constructor
A new instance of WhenExpectationTarget.
-
#not_to(matcher = nil, message = nil, &block) ⇒ Boolean
(also: #to_not)
When expected, it will run the given expectation, passing if
matcherreturns false. -
#to(matcher = nil, message = nil, &block) ⇒ Boolean
When expected, it will run the given expectation, passing if
matcherreturns true. -
#with_inverse ⇒ Object
Indicates whether the inverse of the expectation should be applied should the
whenflag be false.
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
#target ⇒ Object (readonly)
Returns the target of the expectation.
30 31 32 |
# File 'lib/rspec/expectations/when_expectation_target.rb', line 30 def target @target end |
#when_expected ⇒ Boolean (readonly)
Returns 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_to ⇒ Object
49 |
# File 'lib/rspec/expectations/when_expectation_target.rb', line 49 alias_method :expect_not_to, :not_to |
#expect_to ⇒ Object
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.
82 83 84 85 86 87 88 |
# File 'lib/rspec/expectations/when_expectation_target.rb', line 82 def not_to(matcher = nil, = nil, &block) if @when_expected super elsif @with_inverse expect_to matcher, , &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.
63 64 65 66 67 68 69 |
# File 'lib/rspec/expectations/when_expectation_target.rb', line 63 def to(matcher = nil, = nil, &block) if @when_expected super elsif @with_inverse expect_not_to matcher, , &block end end |
#with_inverse ⇒ Object
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 |