Module: RSpec::Matchers
- Defined in:
- lib/saharspec/matchers/ret.rb,
lib/saharspec/matchers/dont.rb,
lib/saharspec/matchers/be_json.rb,
lib/saharspec/matchers/eq_multiline.rb,
lib/saharspec/matchers/send_message.rb
Instance Method Summary collapse
-
#be_json(expected = Saharspec::Matchers::BeJson::ANY) ⇒ Object
be_json
checks if provided value is JSON, and optionally checks it contents. -
#be_json_sym(expected = Saharspec::Matchers::BeJson::ANY) ⇒ Object
be_json_sym
checks if value is a valid JSON and parses it withsymbolize_names: true
. -
#dont ⇒ Object
Negates attached matcher, allowing creating negated matchers on the fly.
-
#eq_multiline(expected) ⇒ Object
Allows to pretty test multiline strings with complex indentation (for example, results of code generation).
-
#ret(expected) ⇒ Object
ret
(short forreturn
) checks if provided block returns value specified. -
#send_message(target, method) ⇒ Object
Checks if the (block) subject sends specified message to specified object.
Instance Method Details
#be_json(expected = Saharspec::Matchers::BeJson::ANY) ⇒ Object
be_json
checks if provided value is JSON, and optionally checks it contents.
If you need to check against some hashes, it is more convenient to use be_json_sym
, which
parses JSON with symbolize_names: true
.
90 91 92 |
# File 'lib/saharspec/matchers/be_json.rb', line 90 def be_json(expected = Saharspec::Matchers::BeJson::ANY) Saharspec::Matchers::BeJson.new(expected) end |
#be_json_sym(expected = Saharspec::Matchers::BeJson::ANY) ⇒ Object
be_json_sym
checks if value is a valid JSON and parses it with symbolize_names: true
. This
way, it is convenient to check hashes content with Ruby's short symbolic keys syntax.
See #be_json_sym for examples.
101 102 103 |
# File 'lib/saharspec/matchers/be_json.rb', line 101 def be_json_sym(expected = Saharspec::Matchers::BeJson::ANY) Saharspec::Matchers::BeJson.new(expected, symbolize_names: true) end |
#dont ⇒ Object
Negates attached matcher, allowing creating negated matchers on the fly.
While not being 100% grammatically correct, seems to be readable enough.
78 79 80 |
# File 'lib/saharspec/matchers/dont.rb', line 78 def dont Saharspec::Matchers::Not.new end |
#eq_multiline(expected) ⇒ Object
Allows to pretty test multiline strings with complex indentation (for example, results of code generation).
In provided string, removes first and last empty line, trailing spaces and leading spaces up
to |
character.
If you need to preserve trailing spaces, end them with another |
.
38 39 40 |
# File 'lib/saharspec/matchers/eq_multiline.rb', line 38 def eq_multiline(expected) Saharspec::Matchers::EqMultiline.new(expected) end |
#ret(expected) ⇒ Object
There is a case when ret
fails: when it is not the first in a chain of matchers joined
by .and
. That's not exactly the matchers bug, that's how RSpec works (loses block's return
value passing the block between matchers)
ret
(short for return
) checks if provided block returns value specified.
It should be considered instead of simple value matchers (like eq
) in the situations:
- Several block behaviors tested in the same test, joined with
.and
, or in separate tests - You test what some block or method returns with arguments, using #its_call
Values are tested with ===
, which allows chaining other matchers and patterns to the check.
99 100 101 |
# File 'lib/saharspec/matchers/ret.rb', line 99 def ret(expected) Saharspec::Matchers::Ret.new(expected) end |