我正在尝试编写用于测试上传功能的规范,并且代码实现按预期工作,但是当我尝试编写规范时,我无法弄清楚为什么数据对话在JSON.parse
. [导轨 5.X]
方法
def upload
#some validation
begin
puts params[:file]
json = JSON.parse(params[:file].read)
#rest of the validation
rescue StandardError, JSON::ParserError, HttpServices::BadHttpResponseError
flash[:style] = :error
end
end
规格:
describe "upload" do
before do
read = file_fixture("empy_details.json").read
@file = Hash.new
@file['emp'] = read #debugger > @file:{emp: [{"name":"Bob","key":"201","active":true}]}
end
it 'should upload' do
post :upload, params: { :file => @file }, as: :json
expect(flash[:style]).to eq(:success)
end
end
该方法puts params[:file]
打印
{"emp"=>"[{\"name\":\"Bob\",\"key\":\"201\",\"active\":true}]\n"}
方法JSON.parse
失败并在失败之前获取值。convert_hashes_to_parameters(key, value)
converted
"[{"name":"Bob","key":"201","active":true}]"
我错过了什么?