Duck Typer is a Ruby gem that validates interface compatibility across polymorphic classes sharing the same role. The post argues for the value of interface tests alongside behavior tests, explaining that while Duck Typer tests alone are fragile, they complement behavior tests by providing targeted, actionable error messages when interface drift occurs. Key benefits include catching Null Object drift, communicating design intent, and prompting better design decisions when public methods diverge across classes. The post also addresses common objections: that behavior tests already catch mismatches (they do, but with less clarity), that it's similar to shoulda-matchers (it's cross-cutting across a group of objects, not single-object), and that runtime checks are preferable (CI pipelines serve as Ruby's equivalent of a compiler). Duck Typer deliberately stays in the test suite, keeping production code clean.

7m read timeFrom thoughtbot.com
Post cover image
Table of contents
“Interface tests are fragile, so you shouldn’t write them”“But I already have behavior tests that catch mismatches”It’s about quality of lifeIt communicates design intent as actionable errorsIt helps you think about design“But that’s just like shoulda-matchers”“But it doesn’t catch errors in production”Wrapping upIf you enjoyed this post, you might also like:

Sort: