Improving Rails test helpers

A great productivity comes from the small things. When making sure that the app works fine on the new version of Rails, I had to fix failures like this one many many times:

PostsControllerTest#test_whatever
    Expected response to be a <200: ok>, but was a <422: Unprocessable Entity>.
Expected: 200
  Actual: 422

The test was usually fairly easy and it looked like this:

class PostsControllerTest < ActionDispatch::Integration
  def test_whatever
    post :create, params: { post: { title: "title" } }, format: :json, as: :json
    assert_response :ok
  end
end

Every time I wanted to look inside, I had to insert a debugger (pry) before the assert_response line and check what kind of errors are present the response body.

I thought: what can I do to make it easier? And I came up with this patch:

module BetterAssertResponse
  def assert_response(*args)
    super
  rescue Minitest::Assertion => e
    if response.body.size < 200
      better_message = "#{e.message}\nResponse body: #{response.body}"
      raise Minitest::Assertion, better_message
    else
      raise
    end
  end
end
ActionDispatch::IntegrationTest.prepend(BetterAssertResponse)

Can you guess how it works?

Before:

PostsControllerTest#test_whatever
    Expected response to be a <200: ok>, but was a <422: Unprocessable Entity>.
Expected: 200
  Actual: 422

After:

PostsControllerTest#test_whatever
    Expected response to be a <200: ok>, but was a <422: Unprocessable Entity>.
Expected: 200
  Actual: 422
Response body: {"errors":["Invalid settings object for section '1'"]}

This trick saves me and other developers a few seconds every time we need to fix a failing test. Counting the number of failing tests we had after upgrading Rails (> 1000), this potentually saves hours of work.

Good news: I’ve submitted a patch to Rails to bring this upstream.

You can do the same trick with ActionController::TestCase in your app. Unfortunatelly, we can’t bring it into Rails because ActionController testing has been deprecated.

About the author

Kir Shatrov Kir Shatrov helps businesses to grow by scaling the infrastructure. He likes to write about software, scalability and interesting stories that he runs into at work. Follow him on Twitter to get the latest updates: @kirshatrov.

Comments

comments powered by Disqus