All Lessons / Fixing Brittle Tests: Avoid Hardcoded Values

Fixing Brittle Tests: Avoid Hardcoded Values

#324 ยท December 23, 2019

Your Teacher | Chris Oliver

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

Discussion

Loading...
logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2020 GoRails, LLC. All rights reserved.