Sending JSON as an attribute in a RSpec Request Test

I haven’t posted in quite a while. Been working with a lot of RSpec request testing for controllers, graphql, and active admin. (Never forget the S in RSpec is capitalized – that would have saved me a lot of pain!!)

My current trouble was sending an attribute that required a json value to an active admin create route via RSpec Request. I tried a whole bunch of things, but the solution is sooo simple, (too simple for the time spent on it) that I had to post it in case I needed to look up what I did again.

I just had to add .to_jsonΒ on that specific attribute requiring json.

  let(:valid_attributes) do
    attributes_for(
      :model,
      content: {"key"=>"value"}.to_json
    )
  end

I hope this helps someone πŸ™‚

I need to start posting more, just when you find the solution after debugging for so many hours it feels too simple to post about. Like I should have known that already! But I think we all have those moments!

Happy Programming πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright Loving to Code 2025
Tech Nerd theme designed by Siteturner