Search⌘ K
AI Features

Implementing a Basic Confidence-Checking System

Explore how to create a confidence-checking system in Rails controller tests to distinguish between setup checks and real assertions, enhancing test reliability. Understand when controller tests add value versus relying on system tests to avoid duplication, and learn techniques to handle test failures with custom exceptions and clearer error messages.

We could just throw #CONFIDENCE CHECK before these assertions, but we don’t think this sort of code comment is nearly as useful as actual code. Let’s make a method that will indicate which assertions are checking that we can run our test and which assertions are the actual test.

We’ll do that by assuming the existence of a method called confidence_check that takes a block and executes the code inside that block.

Ruby
# test/controllers/widgets_controller_test.rb
× # refute_nil widget
× # assert_redirected_to widget_path(widget)
→ confidence_check do
→ refute_nil widget
→ assert_redirected_to widget_path(widget)
→ end

Now, the test makes it clear that refute_nil and assert_redirected_to are only there to double-check that the basics are working before we do the real assertion, which follows.

In addition to demarcating the code, we need to see a helpful error in our test output, letting us know that the test effectively wasn’t even run because of factors ...