RSpec控制器规范的代码如下:
require 'spec_helper'
describe Api::PostsController do
let!(:post) { create(:post) }
describe 'index' do
it 'should return a json array of posts' do
get :index, format: :json
puts "response=#{ response.body }"
result = JSON.parse(response.body)
expect(result[0][:title]).to eq(post.title)
end
end
end
控制器代码:
class Api::PostsController < ApplicationController
def index
@posts = Post.all
end
end
但是我收到了以下错误:
Failure/Error: result = JSON.parse(response.body)
JSON::ParserError:
A JSON text must at least contain two octets!
响应的正文是空的。那么,请告诉我,我该如何解决?提前致谢。
答案 0 :(得分:1)
尝试添加
def index
@posts = Post.all
render :json => @posts
end