Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test Doubles with RSpec

Test Doubles with RSpec

A brief introduction to the core concepts of mocking and stubbing in RSpec::Mocks 3.0

Avatar for Jeremy Croteau

Jeremy Croteau

July 15, 2014

Other Decks in Programming

Transcript

  1. T E S T D O U B L E

    S W I T H R S P E C J E R E M Y C R O T E A U
  2. T E S T D O U B L E

    S • Why do we need them? • What are they? • Test doubles • Method stubs • Message expectations • Partial test doubles
  3. W H Y D O W E N E E

    D T E S T D O U B L E S ? A B D C E F Dependencies G H K I L J M N O P
  4. W H Y D O W E N E E

    D T E S T D O U B L E S ? A B D C E F Application Test A B C E D O U B L E D O U B L E D O U B L E S U T G
  5. W H AT A R E T E S T

    D O U B L E S ? • Pretend objects that stand in for real dependencies of the SUT. • In the literature, also referred to as mocks, stubs, fakes, spies, impostors, dummies, or other names depending on how they are used.
  6. W H AT A R E T E S T

    D O U B L E S ? • Within the context of RSpec::Mocks, these are distilled down to few underlying concepts: ! ! ! ! Object level: Test double Partial test double Method level: Method stub Message expectation
  7. T E S T D O U B L E

    • Test double is a generic term for any object that stands in for a real object during a test (think "stunt double”). ! ! Note: The string argument is optional but recommended, because it is used in failure messages. • Once you have a test double, you can allow or expect messages on it… ! thingamajig = double(‘thingamajig’) Syntax
  8. M E T H O D S T U B

    • A method stub is a method that we can program to return a predefined response during the execution of a test. ! allow(object).to receive(:message).and_return(value) Syntax
  9. M E S S A G E E X P

    E C TAT I O N • A message expectation is a method stub that will raise an error if it is never called during the test. ! expect(object).to receive(:message) Syntax
  10. E X : D O U B L E &

    E X P E C T
  11. E X : M I X I N G S

    T U B S & E X P E C TAT I O N S
  12. PA R T I A L T E S T

    D O U B L E • A partial test double is an extension of a real object in a system that is instrumented with test-double like behavior.
  13. F U R T H E R R E A

    D I N G • The RSpec Book - David Chelimsky • https://github.com/rspec/rspec-mocks • https://www.relishapp.com/rspec/rspec-mocks/v/3-0/docs