Fixing Brittle Tests: Avoid Hardcoded Values

December 23, 2019

Track your progress

Sign in to track your progress and access subscription-only lessons.

Log In

Your Teacher

Hi, I'm Chris. I'm the creator of GoRails, Hatchbox.io and Jumpstart. I spend my time creating tutorials and tools to help Ruby on Rails developers build apps better and faster.

About This Episode

Test suites often become brittle over time where small changes end up breaking various tests. One way to avoid that is by avoiding using hardcoded values in your test suite.

Notes

Sometimes it feels like you make a small change and it breaks a bunch of tests. These are called "brittle tests". Any little thing might cause them to fail. That's the last thing you want in a test suite. You want them to be reliable, and robust against changes.

One of the reason tests become brittle is because you're hardcoding values. Whether that's names, email addresses, or other content you're asserting, the result is the same: constantly broken tests.

Small changes should not affect your test suite, especially small tweaks to content or fixture data. What ends up happening is that our tests may assert that our code works but they ALSO assert that our fixture data is a very specific value.

Instead, we can make our test asserts validate dynamic values. This makes sure they only test our code, not the content or our fixture data. It makes things more reliable and way easier to maintain.

Resources

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.