Module: RSpec::Matchers
- Included in:
- Moarspec::Matchers::BeJson, Moarspec::Matchers::Not::Delegator
- Defined in:
- lib/moarspec/matchers/ret.rb,
lib/moarspec/matchers/dont.rb,
lib/moarspec/matchers/be_json.rb,
lib/moarspec/matchers/eq_multiline.rb,
lib/moarspec/matchers/send_message.rb
Instance Method Summary collapse
-
#be_json(expected = Moarspec::Matchers::BeJson::ANY) ⇒ Object
be_jsonchecks if provided value is JSON, and optionally checks it contents. -
#be_json_sym(expected = Moarspec::Matchers::BeJson::ANY) ⇒ Object
be_json_symchecks if value is a valid JSON and parses it with ‘symbolize_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 = Moarspec::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/moarspec/matchers/be_json.rb', line 90 def be_json(expected = Moarspec::Matchers::BeJson::ANY) Moarspec::Matchers::BeJson.new(expected) end |
#be_json_sym(expected = Moarspec::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/moarspec/matchers/be_json.rb', line 101 def be_json_sym(expected = Moarspec::Matchers::BeJson::ANY) Moarspec::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.
94 95 96 |
# File 'lib/moarspec/matchers/dont.rb', line 94 def dont Moarspec::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/moarspec/matchers/eq_multiline.rb', line 38 def eq_multiline(expected) Moarspec::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
{Moarspec::Its::Call#its_call #its_call}
Values are tested with ‘===`, which allows chaining other matchers and patterns to the check.
99 100 101 |
# File 'lib/moarspec/matchers/ret.rb', line 99 def ret(expected) Moarspec::Matchers::Ret.new(expected) end |
#send_message(target, method) ⇒ Object
Checks if the (block) subject sends specified message to specified object.
169 170 171 |
# File 'lib/moarspec/matchers/send_message.rb', line 169 def (target, method) Moarspec::Matchers::SendMessage.new(target, method) end |