Skip to content

Response is not rendered in requests specs with Rails 7.2 and ActiveModelSerializers #2806

@januszm

Description

@januszm

What Ruby, Rails and RSpec versions are you using?

Ruby version: 3.2.5
Rails version: 7.2.1
RSpec version: rspec-rails 7.0.1 , rspec-core 3.13.1

Observed behaviour

Before upgrade to Rails 7.1.2, both response and response.parsed_body returned properly rendered JSON for an API that uses Rails ActiveModelSerializers. After the upgrade, response contains the ActiveModelSerializers object simply returned as string, instead of the actual render.

> response
=> ...
 @stream=
  #<ActionDispatch::Response::Buffer:0x0000000135a98810
   @buf=["\"#<ActiveModelSerializers::Adapter::Attributes:0x0000000135add410>\""],

> response.body # or response.parsed_body, same result
=> "#<ActiveModelSerializers::Adapter::Attributes:0x000000012149dd70>"

Expected behaviour

In requests specs, response from a test for an API that uses ActiveModelSerializers and returns JSON, should return properly rendered JSON object, instead it returns the serializer instance.

Can you provide an example reproduction?

I created a brand new Rails 7.2 application with ActiveModelSerializers, but with stadard Rails setup and it properly renders the JSON object in the Integration test

test/integration/api_test.rb:7 ExampleTest#test_api:

    4: def test_api
    5:   Property.create(address: "155 Jackson", city: "Chicago")
    6:   get "/properties"
 => 7:   binding.pry
    8: end

> response
 @stream=
  #<ActionDispatch::Response::Buffer:0x0000000106629100
   @buf=
    ["[{\"address\":\"MyString\",\"city\":\"MyString\"},{\"address\":\"123 Jackson\",\"city\":\"Chicago\"}]"],

> response.parsed_body
=> [{"address"=>"MyString", "city"=>"MyString"}, {"address"=>"155 Jackson", "city"=>"Chicago"}]

which proves this is not an issue with ActiveModelSerializers, because it produces proper results with standard Rails 7.2.1 test suite. Not sure if this is an issue with rspec-rails or another gem from rspec group, so posting it here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions