Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:54:08+00:00 2026-06-17T20:54:08+00:00

I’m trying to write a set of rspec tests for a JSON API written

  • 0

I’m trying to write a set of rspec tests for a JSON API written in Rails 3.2 with Backbone as the front end. The tests I’m writing are specifically for the Rails controllers.

Now, the application itself is working fine. When a client issues a PUT request with the body:

{
  "id":1,
  "name":"updated product set",
  ...
}

Everything is great. In the background Rails will take that body, then transform it into:

{
  "id":1,
  "name":"updated product set",
  ...
  "model_name" => { ... }
}

where the hash “model_name” points to contains the attribute values it can automatically figure out from the input. All is well.

When I try the same exact request from the testing environment, all is not well. If I create a hash that is exactly the same PUT body hash as above, but in rspec:

@update_json = {
  "id":1,
  "name":"updated product set",
  ...
}
header "Accept","application/json"
header "Content-type","application/json"
put :update, @update_json

Things do not work out at all. In the controller, if I inspect the params variable I get:

{ "model_name" => {} }

If I omit the “Content-type” header line, I get instead @update_json, but without the Rails manipulation that creates the “model_name” mapping which I require in my controller.

The only way I’ve gotten a successful test to run is the following:

@update_json = {
  :format => "json",
  "id":1,
  "model_name" => {
      "id":1,
      "name":"updated product set",
      ...
  }
}
header "Accept","application/json"
put :update, @update_json

However, this is not a real-world test since it’s not testing the exact PUT body that would be sent by my backbone front-end.

Any ideas?

UPDATE:
From looking around it looks like you can’t actually do a real HTTP request from rspec; it’s just mocked up. For example, you can’t get the system to respond with a 404 since Rails throws an Exception that would normally be caught by Rails network stack and turned into a 404 but which isn’t with Rspec. So the problem looks deeper than what’s above…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:54:10+00:00Added an answer on June 17, 2026 at 8:54 pm

    I was having the same problem. No matter what I did, or which ways I specified the content type as ‘application/json’, it just wouldn’t work. If there was a suggestion on the internet for setting the content type to json, I tried it. I even tried them all together at the same time.

    Ultimately, I traced it to ActionController::ParamsWrapper, where _wrapper_enabled? was always returning false (due to request.content_mime_type being nil). This solution works for me. I was looking for the ‘magical’ params key to get added as well and this does it.

    mime_type = mock
    mime_type.stub :ref => :json
    request.stub :content_mime_type => mime_type
    request.accept = 'application/json'
    post :create, widget.as_json
    

    The values are:

    widget.as_json #=> {
      "created_at"=>nil,
      "description"=>"Description 1 - Quidem nihil quae aliquid sed qui.",
      "id"=>nil,
      "order"=>1,
      "title"=>"Title 1 - ut",
      "updated_at"=>nil
    }
    
    # params hash in the controller.
    params #=> {
      "created_at"=>nil,
      "description"=>"Description 1 - Quidem nihil quae aliquid sed qui.",
      "id"=>nil,
      "order"=>"1",
      "title"=>"Title 1 - ut",
      "updated_at"=>nil,
      "controller"=>"api/widgets",
      "action"=>"create",
      "widget"=>{
        "title"=>"Title 1 - ut",
        "order"=>"1",
        "description"=>"Description 1 - Quidem nihil quae aliquid sed qui."
      }
    }
    

    This is exactly what I wanted. Only title, order, and description are set as attr_accessible, hence the only attributes that appear in the magically created :widget hash.

    I am also using active_model_serializers, so the as_json is running through that, in case anyone thinks it matters.

    This is part of a public example app I’m building, so this code will be able to be seen in use, if anyone thinks it might be useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am confused How to use looping for Json response Array in another Array.
I am using JSon response to parse title,date content and thumbnail images and place
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.