Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

response.body in Rspec controller test returns an empty string

I'm trying to build a restful JSON api in Rails using the Devise gem and I have the following code:

users_controller_spec.rb:

require 'rails_helper'

RSpec.describe Api::V1::UsersController, type: :controller do

    describe "PUT/PATCH #update" do
    render_views
    context "when is successfully updated" do

      before(:each) do
        @user = FactoryGirl.create :user
        request.headers['Authorization'] = @user.auth_token
      end

      it "renders the json representation for the updated user" do
        user_response = json_response
        expect(user_response[:email]).to eq "[email protected]"
      end
  end
end

The issue I have is that response.body equals an empty string, which isn't valid JSON and hence I can't do JSON.parse(response.body)

I read elsewhere on SO to include render_views and you can see I've done that, but it makes no difference.

Please let me know if you need more info

like image 618
BillyBib Avatar asked Aug 02 '26 00:08

BillyBib


1 Answers

In my case, the result was raising a permission error with a blank body because I didn't have a logged in user. Check that your response is a success first.

like image 104
Ponny Avatar answered Aug 03 '26 17:08

Ponny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!