Published: September 29, 2022 • Updated: June 28, 2023 • 2 min read
When testing a serialized Rails API with RSpec, a common mistake is 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. This is 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
What are your thoughts on this? Let me know!
Join 100+ engineers who subscribe for advice, commentary, and technical deep-dives into the world of software.