【发布时间】:2018-12-27 18:10:48
【问题描述】:
我正在尝试测试 Rails API 的简单控制器操作
这是有问题的控制器:
class Api::TransactionsController < ApplicationController
def index
transactions = Transaction.all
json = TransactionSerializer.render(transactions)
render json: json
end
end
这是我目前的规格
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
我想测试响应。类似于这个 Stack Overflow 问题How to check for a JSON response using RSpec?:
specify { expect(response.body).to eq([].to_json) }
我的问题是response.body 是一个空字符串。这是为什么 ?
【问题讨论】:
-
您对
response.body的期望是什么?此外,更好的做法是拥有expect(response).to have_http_status(:success)。 -
因为你在
test环境下的数据库是空的。 -
是的,数据库是空的,所以响应应该是一个空数组。此外,正如您在规范中看到的那样,我将序列化程序存根以返回一个空数组。
-
我认为你需要在
before块中调用get :index,否则没有响应体。 -
是的,它有效并且有意义。谢谢
标签: ruby-on-rails ruby rspec rspec-rails