Definition Spec in Ruby – RSpec implementation

12 marca 2025
ruby

The Infamous "S" Incident

Imagine this: You're working on a crucial business feature in Ruby on Rails. Your team nails it in 3 days. It's perfect. You deploy. Management pops the champagne, Marketing fires up AdWords. Success! 🎉🔥💪

An hour later, a colleague deploys a feature he's been working on for 3 weeks. It could've waited another day, but hey, QA was busy with your feature, and it's just some dashboard views and queries. What could go wrong?

The pipelines are green, but... the app's down. Puma won't start. Disaster. Clients are calling, support's panicking, champagne's going flat.

Turns out, a single, lonely "s" snuck into a class file. Probably a slip of the finger. Code review missed it, QA didn't catch it, worked fine locally, but took down production.

Sounds crazy? It's a true story from my friend's project.

The Culprits

  • Interpreted Languages: No build phase to catch syntax errors.
  • Ruby on Rails Quirks: Lazy loading in dev/test environments, but not in production.
  • Lack of Test Coverage: The problematic class had no tests.
  • The "No Tests" Excuse

    We've all heard it:

  • "The client won't pay for tests"
  • "No time, deadlines are tight"
  • "It's a simple feature, what's to test?"
  • The Lesson

    One of my favorite work philosophies: "Know-how is important, but knowing how not to is most important."

    The Solution: Definition Spec

    If you can't write full tests, at least write one that'll save your bacon in cases like this.

    The Definition Spec

    It's all about writing your first test when creating a new class to check if the class is actually defined. This forces it to load in test mode. It's just 4 lines, but they provide a minimum safety net and protect against typos. The Definition Spec in Ruby seems tailor-made for this issue.

    Take a look:

    I created a module to avoid typing the same code over and over. You can also use a custom matcher. Here are some examples:

    Custom Module:

    Defining your own matcher

    In TDD, this guarantees a motivational dopamine hit. You write the definition spec. Red. You add the file and define the class. Green. And suddenly you're eager to keep working 😀

    If you have time and inclination, you can test whether the public methods you plan to use are defined. No mocking, no complex scenarios. Just minimal coverage that protects against trivial screw-ups.

    Just as startups have MVP - Minimum Viable Product, I propose MVC, but as Minimally Verified Class. Introducing definition spec in Ruby to your routine can provide this.

    A wise person from the Ruby world (Andrzej Krzywda - broader context here) said he works in Ruby because he likes fixing typos in production.

    Let's not fix typos in production. Let's do it earlier, much earlier.

    After adding a definition spec, tests fail if there's an unwanted letter in the tested class.

    Write definition specs in Ruby, seriously.