Published: September 29, 2022 • 2 min read
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
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 Jake Worth's Newsletter for bi-weekly ideas, creations, and curated resources from across the world of programming. Join me today!
Blog of Jake Worth, software engineer in Maine.