How do we print response body in rspec

Viewed 1142

I am trying to write an rspec file for my meetings_controller.rb so as to check if the values returned from my database are correct.

When I go to localhost:3000/meeting.json, this is the result of my data from the database

enter image description here

my rspec file is trying to check if the correct values are returned.

I created a folder called controller under specs (after I have installed rspec)and have the file meeting_controller_spec.rb

require 'rails_helper'

# Change this ArticlesController to your project
RSpec.describe MeetingsController, type: :controller do

    describe "GET #index" do
        # check index
        it "returns a success response" do
            get :index
            puts response.body
            expect(response).to have_http_status(:ok)
            
        end

    end
end

I tried to print the response body but nothing is returning. Is there a way to do this?

(base) adam-a01:reservation adrianlee$ rspec

.

Finished in 0.0892 seconds (files took 12.15 seconds to load)
1 example, 0 failures

Btw this is my meeting_controller.rb

class MeetingsController < ApplicationController
  before_action :set_meeting, only: [:show, :edit, :update, :destroy]

  # GET /meetings
  # GET /meetings.json
  def index
    @meetings = Meeting.all
    @meeting = Meeting.new

  end
end

Update: I also tried this method as suggested below but it didnt work still

# Change this ArticlesController to your project
RSpec.describe MeetingsController, type: :controller do

    describe "GET #index" do
        # check index
        it "returns a success response" do
            get :index
            raise response.body 
            expect(response).to have_http_status(:ok)
            
        end

    end
end

This is the error raised Failures:

  1) MeetingsController GET #index returns a success response
     Failure/Error: raise response.body
     RuntimeError:
     # ./spec/controllers/meeting_controller_spec.rb:10:in `block (3 levels) in <top (required)>'
3 Answers

If you are trying to get this value for a debugging purpose you should use byebug or pry gems.

If you choose byebug, add it to your gemfile gem 'byebug' on test environment. run bundle install and after that you are able to use it on your tests

So replace the puts with byebug

describe "GET #index" do
   # check index
   it "returns a success response" do
     get :index
     byebug
     expect(response).to have_http_status(:ok) 
       
   end

end

At the console now you are exactly there. Just type response.body and enter to print it's value. When you are done, just type c and then enter to release the console e continue your tests.

Also check here for more information about debugging with byebug

In my case I do it like this:

require 'rails_helper'

describe SuppliersController, type: :controller do
  describe 'GET /suppliers' do
    let!(:access_token) { create :access_token }
    let!(:admin_user_token) do
      create :user_token, resource_owner: create(:admin_user)
    end

    context 'when the requester is an admin' do
      it 'returns HTTP status 200 (OK)' do
        allow(controller).to receive(:doorkeeper_token) { access_token }
        @request.env['HTTP_X_USERTOKEN'] = admin_user_token.token

        get :index

        raise response.body.inspect ## Here is what prints the value in the console.

        body = JSON.parse response.body

        expect(response).to have_http_status :ok
        expect(body['status']).to eq 'OK'
      end
    end
  end
end

To run the test:

rspec spec/controllers/suppliers/suppliers_controller_index_spec.rb 

And it gives me the output:

F

Failures:

  1) SuppliersController GET /suppliers when the requester is an admin returns HTTP status 200 (OK)
     Failure/Error: raise response.body.inspect
     
     RuntimeError:
       "{\"status\":\"OK\",\"message\":\"Your request has been processed successfully.\",\"data\":[],\"page\":{\"current_page\":1,\"prev_page\":null,\"next_page\":null,\"per_page\":25,\"total_pages\":0}}"
     # ./spec/controllers/suppliers/suppliers_controller_index_spec.rb:17:in `block (4 levels) in <top (required)>'

Finished in 0.47594 seconds (files took 2.82 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/suppliers/suppliers_controller_index_spec.rb:11 # SuppliersController GET /suppliers when the requester is an admin returns HTTP status 200 (OK)

I have to say raise a runtime error to check variable value is not a good practice,maybe you should try gem 'pry' or gem 'byebug' suggested by @DR7. Another thing you need make sure is did you run rails s and rspec in different environment?This maybe lead to they connect to separate db.

Related