1 / 7

RSpec Matchers, Stubs and Mocks

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.

bianca
Download Presentation

RSpec Matchers, Stubs and Mocks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RSpec Matchers,Stubs and Mocks

  2. 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)

  3. 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

  4. (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)

  5. Collections and RSpec

  6. 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

  7. 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

More Related