~/talk

To mock, or not to mock?

Talkat RubyConf 2021 · 2021-11-08..11-10· [testing]
links:none on file
slides:none on file

Concrete rule set: use mocks when one of three questions is true — the test uses expensive resources, the code is non‑deterministic, or the component’s state can’t/shouldn’t be inspected. Examples use RSpec and rspec-mocks, a client pattern around a Shakespeare API, a custom ShakespeareError, an extractable ruby_words module for randomness, and a fake_sphere gem that caches quotes; mocks replace the external client or force errors so tests stay fast, deterministic, and decoupled.

AI-written summary · about

  1. 0:00Mocking in testing intro
  2. 1:23What mocks are and challenges
  3. 3:29Classical vs mockist testing
  4. 6:57Risks of over-mocking
  5. 7:17When mocks add value
  6. 9:01Preventing expensive resources
  7. 14:16Deterministic tests for non-deterministic code
  8. 18:04Testing caches and hidden state
  9. 25:08When to mock framework
  10. 26:52Closing thoughts
~/search
type to search 6,249 conferences, 138,987 speakers and 185,172 talks
~/help
cmdctrl k
search every conference, speaker and series
/
focus the filter search
h
home
c
conferences
s
speakers
e
series
t
light / dark — the status bar button does the same
?
this help
esc
close this, then clear every filter — or just the search, while you are typing in it

Ignored while you are typing. Pages without a filter rail have no search to focus.