Test Doubles Test Doubles Test Doubles Test Doubles Test Doubles Dummy: Just a value Stub: Returns canned answers Fake: Simpler implementation Mock: Has interaction-verification baked in Spy: Can be asked questions about interactions later Martin Fowler: Mocks aren't Stubs @dtanzer 23
Fake: Something, but not Real Fake: Something, but not Real Fake: Something, but not Real Fake: Something, but not Real Fake: Something, but not Real @dtanzer 32
Spy: I Know what You Did to Me! Spy: I Know what You Did to Me! Spy: I Know what You Did to Me! Spy: I Know what You Did to Me! Spy: I Know what You Did to Me! @dtanzer 38
Mock: Use Correctly or Blow Up Mock: Use Correctly or Blow Up Mock: Use Correctly or Blow Up Mock: Use Correctly or Blow Up Mock: Use Correctly or Blow Up @dtanzer 39
Result-Based Result-Based Result-Based Result-Based Result-Based Observe result or state Verify result matches the expected result Use dummies, stubs or fakes if you need them expect "Frontdoor testing" Interaction-Based Interaction-Based Interaction-Based Interaction-Based Interaction-Based Nothing to observe Verify expected interaction has happened Use mocks and/or spies; and dummies, stubs or fakes if you need them when / verify Backdoor testing @dtanzer 41
Special Case: Jest Mock ES-Module Special Case: Jest Mock ES-Module Special Case: Jest Mock ES-Module Special Case: Jest Mock ES-Module Special Case: Jest Mock ES-Module "^(.*)/MonacoEditor$": "/__mocks__/reactMonacoMock.js", module.exports = {
Mock / Fake Close to DMZ Mock / Fake Close to DMZ Mock / Fake Close to DMZ Mock / Fake Close to DMZ Mock / Fake Close to DMZ it('loads 2 designs when 2 designs are found', () => {
7. Don't Mock what You don't Own 7. Don't Mock what You don't Own 7. Don't Mock what You don't Own 7. Don't Mock what You don't Own 7. Don't Mock what You don't Own
8. Integration-Test at Boundary 8. Integration-Test at Boundary 8. Integration-Test at Boundary 8. Integration-Test at Boundary 8. Integration-Test at Boundary
Boundary AND Hard to Test Boundary AND Hard to Test Boundary AND Hard to Test Boundary AND Hard to Test Boundary AND Hard to Test Electron IPC Electron IPC Electron IPC Electron IPC Electron IPC sendSync(channel: C_MAIN, ...args: any[]) {
1. Tradeoffs Everywhere 2. Powerful, Dangerous Tool 3. No Mocks: Error Boundary 4. Make Test-Doubles Visible 5. Fake DMZ / Outside World 6. Result- -or- Interaction-based 7. Don't Mock what You don't Own 8. Integration-Test at Boundary 9. Leverage the Type System @dtanzer 58
Heuristics: Mocks Heuristics: Mocks Heuristics: Mocks Heuristics: Mocks Heuristics: Mocks Do not mock what you don't own Avoid mocks to make untestable code testable Avoid mocks that return mocks Avoid expect and verify in the same test Don't mix result-based and interaction-based testing @dtanzer 59