Jake Worth

Jake Worth

How to Assert About Response Body in RSpec Controller Tests

Published: September 29, 2022 2 min read

  • ruby

When testing a serialized Rails API with RSpec, a common mistake it to assert about the response body, only to find that it’s an empty string. The controller is controlling, the view is presenting. What’s missing?

What’s missing is the view: RSpec views are stubbed by default. Ideal for most controller tests. Just not those asserting about serialized data.

Render those views with render_views:

require "rails_helper"

RSpec.describe WidgetsController, type: :controller do
  render_views
  describe "GET index" do
    it "has a widgets related heading" do
      get :index
      expect(response.body).to match /<h1>.*widgets/im
    end
  end
end

Get better at programming by learning with me. Subscribe to my newsletter for weekly ideas, creations, and curated resources from across the world of programming. Join me today!