guides icon indicating copy to clipboard operation
guides copied to clipboard

Testing with Mocks

Open mabelleeyanhwa opened this issue 5 years ago • 0 comments
trafficstars

code-along template: https://github.com/thoughtworks-jumpstart/learn-mocking-and-stubbing

lesson outline:

  • write on board: jest.fn(factory), jest.mock(path, factory)
  • what are mocks
    • Mocking is a technique to isolate test subjects by replacing dependencies with objects that you can control. The goal for mocking is to replace something we don’t control with something we do
  • when to mock
  • how to create mocks with jest.fn() and jest.mock()
  • assertions which we can use on mock functions
  • demo
    • fetch
    • payment gateway
    • 2 classes

jest gives us many ways to define a mock. see https://medium.com/@rickhanlonii/understanding-jest-mocks-f0046c68e53c for a comparison (note. it's using ES6 import syntax, so you have to map it to your CommonJS import/export syntax)

recap:

  • benefits of mocking
    • simplify testing (e.g. new Cat(brain, tail, coatOfFur, owner))
    • prevent expensive operations (e.g. fetch or actual payments)
    • simulate all edge cases in a class/function
    • remove dependencies (on data, network requests, other classes, etc)
  • terms
    • mocks, stubs, dummies, spies

mabelleeyanhwa avatar Dec 02 '19 03:12 mabelleeyanhwa