I'm trying to test a simple controller's action of a Rails API
Here's the controller in question:
class Api::TransactionsController < ApplicationController
def index
transactions = Transaction.all
json = TransactionSerializer.render(transactions)
render json: json
end
end
Here are my specs so far
require 'rails_helper'
RSpec.describe Api::TransactionsController do
describe '.index' do
context "when there's no transactions in the database" do
let(:serialized_data) { [].to_json }
before { allow(TransactionSerializer).to receive(:render).with([]).and_return(serialized_data) }
after { get :index }
specify { expect(TransactionSerializer).to receive(:render).with([]) }
specify { expect(response).to have_http_status(200) }
end
end
end
I want to test the response. Something like in this Stack Overflow question How to check for a JSON response using RSpec?:
specify { expect(response.body).to eq([].to_json) }
My problem is that response.body
is an empty string. Why is that ?