Functional testing of a JSON Rails API Functional testing of a JSON Rails API json json

Functional testing of a JSON Rails API


This is a bug, reported by same author of this question. It is not likely to be fixed until Rails 5, or so it seems by looking at the milestone it has been assigned to.

If you land here, like me, after some hours dealing with this issue unknowing that it is really a bug, maybe you want to know that you can do that in an Integration test:

$ rails g integration_test my_integration_test

require 'test_helper'class MyIntegrationTestTest < ActionDispatch::IntegrationTest  setup do    @owner = Owner.create(name: 'My name')    @json = { name: 'name', value: 'My new name' }.to_json  end  test "update owner passing json" do    patch "/owners/#{@owner.id}",       @json,      { 'Accept' => Mime::JSON, 'Content-Type' => Mime::JSON.to_s}    assert_response :success    assert_equal 'application/json', response.headers['Content-Type']    assert_not_nil assigns :owner    assert_equal 'My new name', assigns(:owner).name  endend