【发布时间】:2014-12-16 22:39:17
【问题描述】:
我正在编写一个测试来使用 Rspec 检查对 Rails API 的无效 DELETE 请求。
这就是我所拥有的:
context 'invalid id number' do
it 'returns success: false' do
xhr :delete, :destroy, id: 999999999999999999
expect(JSON.parse(response.body)['success']).to be_false
end
end
Postgres 抛出某种整数溢出异常(应该如此),但在我的规范中,我无法查看 JSON 对象,因为它从未形成。我怎样才能让它返回 { success : false } 而不是空白字符串?尽管出现异常,如何强制 JSON 对象返回?
当我使用 pry 查看 json 对象时,我收到此错误:JSON::ParserError: A JSON text must at least contain two octets! 因为 response.body 的计算结果为空字符串 ""
哇,差点忘了包含控制器代码。
def destroy
if (site == ::MyModel.find(params[:id]))
site.destroy
render :json => {success: true}
else
render :json => {success: false}
end
【问题讨论】:
标签: ruby-on-rails json postgresql rspec