Module: RPicSim::RSpec::PersistentExpectations
- Included in:
- Helpers
- Defined in:
- lib/rpicsim/rspec/persistent_expectations.rb
Overview
This simple module is included in Helpers so it is available in RSpec tests. It provides a way to set expectations on objects that will be checked after every step of the simulation, so you can make sure that the object stays in the state you are expecting it to.
Example:
b = []
expecting b => satisfy { |lb| lb.size < 10 }
20.times do
b << 0
check_expectations # => raises an error once b has 10 elements
end
For more information, see PersistentExpectations.
Instance Method Summary collapse
-
#check_expectations ⇒ Object
Checks the expectations; if any object does not match its matcher then it raises an error.
-
#expectations ⇒ Hash
Returns the current set of persistent expectations.
-
#expecting(hash) ⇒ Object
Adds or removes expectations.
Instance Method Details
#check_expectations ⇒ Object
Checks the expectations; if any object does not match its matcher then it raises an error.
31 32 33 34 35 36 |
# File 'lib/rpicsim/rspec/persistent_expectations.rb', line 31 def check_expectations expectations.each do |subject, matcher| expect(subject).to matcher if matcher end nil end |
#expectations ⇒ Hash
Returns the current set of persistent expectations. The keys are the objects under test and the values are matchers that we expect to match the object.
25 26 27 |
# File 'lib/rpicsim/rspec/persistent_expectations.rb', line 25 def expectations @expectations ||= {} end |
#expecting(hash) ⇒ Object
Adds or removes expectations. The argument should be a hash that associates objects to matchers. A matcher can be any bit of Ruby code that you would be able write in RSpec in place of MATCHER in the code below:
expect(object).to MATCHER
For example, you could do:
expecting main_output_pin: be_driving_high
To remove an expectation on an object, just provide nil
for
the matcher.
If given a block, applies the new expectations, executes the block, then resets expectations to their former state. Expectation blocks may be nested and freely mixed with other calls to `expecting`.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rpicsim/rspec/persistent_expectations.rb', line 55 def expecting(hash) if block_given? saved_expectations = expectations.clone begin expecting(hash) yield ensure @expectations = saved_expectations end else expectations.merge! hash end end |