80 likes | 216 Views
RSpec Matchers, Stubs and Mocks. RSpec matchers. RSpec “hangs” to and not_to methods on all outcome of expectations t o()/ not_to () methods take one parameter – a matcher Matcher examples: be_true / be_false e q 3 raise_error ( SomeError ). be_predicate – boolean.
E N D
RSpec matchers • RSpec “hangs” to and not_to methods on all outcome of expectations • to()/not_to() methods take one parameter – a matcher • Matcher examples: • be_true / be_false • eq 3 • raise_error(SomeError)
be_predicate – boolean • If the object on which the test is operating has a predicate (boolean) method – you automagically get a be_predicate matcher • So, for example be_nil is a valid matcher since every Ruby object has a :nil? method
(More) Syntactic Sugar • Can add “a” or “an” to any matcher name (for readability) • If the predicate method starts with “has”, you can substitute “have” instead • In case of collections, you can • Pass a number you are expecting to have() • Make things up at the end for readability • (See next slide)
RSpec Stubs • Stub – a canned response • double() method reduces the pain of having to manually create stub methods for objects • Simply call double() and pass in methods you want created and their return values
RSpec Mocks • Objects that allow you to set and verify expectations that they will receive specific messages during runtime • Useful for specifying how the subject of the spec interacts with its collaborators