← Graph

Testing Randomness

talk 26 connections

Jakub Rodzik's wroclove.rb 2023 talk, framed as a bedtime story about developer 'Fredo' writing a two-player dice game in Ruby/RSpec. Fredo progresses through four testing iterations: (1) plain conditionals in tests that mirror the production if/else and give false confidence; (2) stubs via `allow(...).to receive(:rand).and_return(...)` which are deterministic and easy but couple tests to implementation; (3) an OCP-inspired refactor extracting a `Dice` abstraction and injecting a `FakeDice` through dependency injection, enabling a 'BetterDice' variant without rewriting tests; (4) adding event/audit logging so tests can assert on the sequence of rolls and winner selection, proving *why* the code behaves as it does. Closes with tradeoffs of each approach and the core recommendation: don't test randomness — understand it, extract it, and control it (e.g. the `Time.current` anti-pattern and 'time travel' fix). Q&A covers unit-vs-end-to-end testing with fakes, alternatives to time-travel helpers (algebraic effects, DI), keeping fake code out of production files, running the same suite against fake and real implementations, and using RBS to keep fake and real signatures in sync.

type
talk
talk Testing Randomness
about
Fredo begins by writing RSpec tests first and labels his approach TDD.
talk Testing Randomness
about
Logic in Tests concept
First iteration of tests uses conditionals that mirror production logic, demonstrating the anti-pattern.
talk Testing Randomness
about
Second iteration stubs `rand` with `allow(...).to receive(:rand).and_return(...)` and the talk analyzes the tradeoffs of stubs and mocks.
talk Testing Randomness
about
Third iteration applies OCP to extract a Dice abstraction so new dice variants don't require modifying the Game class.
talk Testing Randomness
about
Injecting a FakeDice into Game is presented as the clean solution, and DI is named explicitly at the end of that section.
talk Testing Randomness
about
Fake Object concept
FakeDice is introduced as a full class substituted for the real Dice.
talk Testing Randomness
about
Fourth iteration adds an events log on every state change so tests can assert on the execution trace.
talk Testing Randomness
about
Event Sourcing concept
Rodzik notes event sourcing gives the event-log benefit 'for free'.
talk Testing Randomness
about
Duck Typing concept
FakeDice is justified with 'if it looks like a dice and rolls like a dice, it's a dice'.
talk Testing Randomness
about
Closing example: Time.current is a hidden random source and motivates the 'understand, extract, control' prescription.
talk Testing Randomness
about
RSpec tool
All examples are written in RSpec and use `allow`/`receive`/`instance_double`/time-travel helpers.
talk Testing Randomness
about
RBS tool
Audience Q&A suggests RBS to keep fake and production class signatures aligned.
talk Testing Randomness
about
Datadog tool
Mentioned as a possible destination for state-change events/metrics.
talk Testing Randomness
about
Sumo Logic tool
Mentioned as a log sink whose bill can balloon under heavy traffic.
talk Testing Randomness
about
Cited (with 99 Bottles) as a source recommending running the same suite against both fake and real implementations.
talk Testing Randomness
about
Cited alongside POODR as a source for the 'test both fake and real' technique.
asked_at
Testing Randomness talk
Audience question during Q&A.
asked_at
Testing Randomness talk
Audience challenged the 'only option' claim about time-travel helpers.
asked_at
Testing Randomness talk
Audience question during Q&A.
person Jakub Rodzik
authored
Testing Randomness talk
Presented the talk at wroclove.rb 2023.
from_talk
Testing Randomness talk
Closing prescription of the talk.
from_talk
Testing Randomness talk
Summary of the first-iteration analysis.
from_talk
Testing Randomness talk
Summary of the third-iteration analysis.
from_talk
Testing Randomness talk
Summary of the fourth-iteration analysis.
from_talk
Testing Randomness talk
Rodzik's stated personal rule, reinforced by Q&A.
talk Testing Randomness
presented_at
Single-track Ruby conference where the talk was delivered.

Provenance

Created
2026-04-17 16:17 seed
Read by
21 extractions