JSON :: PARSERERROR:意外的令牌AT''在Rails RSPEC上
我在RSPEC上有一个问题。因此,基本上我想要的是向API发送请求,并有适当的响应。当我将数据发送到API端点时,并且i puts
它发送的内容似乎正确地发送了所有数据,但是在响应级别上,它说我没有发送所需的参数。 (我还使用VCR测试)
我的RSPEC:
需要“ rails_helper”,
describe 'Create Vendor', type: :request do
let(:user) { create(:alter_user) }
let(:vendor_attributes) do
{
#These are all the required parameters
vendor: {
token: '',
refcode: '',
request_type: 'renters',
first_name: 'Tony',
last_name: 'Stark',
phone: '6504881234',
phone_type: 'mobile',
email: '[email protected]',
address1: '123 Main Street',
address_city: 'Palo Alto',
address_state: 'CA',
address_zip: '94301',
DOB: '1982-10-27'
}
}
end
after do
VCR.eject_cassette
end
it 'creates a vendor' do
VCR.use_cassette('create_vendor') do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
response_body = JSON.parse(response.body)
expect(response_body['refcode']).to eq('corp-api-test')
end
end
it 'fails if the vendor is missing attributes' do
VCR.use_cassette('create_vendor_missing_attributes') do
post '/api/v2/vendor', params: {vendor: {last_name: 'Stark', phone: '6504881234'}}.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
expect(response.status).to eq(422)
end
end
it 'requires Authorization token header' do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json'}
expect(response.status).to eq(401)
end
end
执行RSPEC FILEPATH时
Create Vendor
Post Request----: {"request_type":"renters","first_name":"Tony","last_name":"Stark","phone":"6504881234","phone_type":"mobile","email":"[email protected]","address1":"123 Main Street","address_city":"Palo Alto","address_state":"CA","address_zip":"94301","DOB":"1982-10-27","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
Response----: {:action=>"new_vendor", :user_hash=>"e59871c35edb7f622c97acfcc20955e9", :result=>"success", :agent_phone=>"888-888-8888", :vendor_uuid=>"242992518092902061", :policy_shell_uuids=>{"RENT"=>"243548182003445420"}, :marketing_headline=>"Test Marketing Headline", :marketing_content=>"<ul><li>Test Marketing Content</li></ul>"}
-----------------------------------------------------
It was successful
creates a vendor (FAILED - 1)
Post Request----: {"last_name":"Stark","phone":"6504881234","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
Response----: {:result=>"error", :message=>"missing fields: first_name is a required field, email is a required field, address1 is a required field, address_city is a required field, address_state is a required field, address_zip is a required field, DOB is a required field, phone_type is a required field"}
-----------------------------------------------------
fails if the vendor is missing attributes
requires Authorization token header
Failures:
1) Create Vendor creates a vendor
Failure/Error: response_body = JSON.parse(response.body)
JSON::ParserError:
809: unexpected token at ''
# ./spec/requests/create_vendor_spec.rb:34:in `block (3 levels) in <top (required)>'
# ./spec/requests/create_vendor_spec.rb:32:in `block (2 levels) in <top (required)>'
# ./spec/spec_helper.rb:85:in `block (3 levels) in <top (required)>'
# ./spec/spec_helper.rb:84:in `block (2 levels) in <top (required)>'
我会收到此信息:这是我的供应商控制器
,因为您可以在这里看到我打印我发送给API的内容,它发送了数据,但它发送了数据,但是然后,它无法创建一个供应商。
def corp_new_vendor_v2
arguments = create_vendor_arguments
https = Net::HTTP.new(VENDOR_CREATE_VENDOR_URI.host, VENDOR_CREATE_VENDOR_URI.port)
https.use_ssl = true
post_request = Net::HTTP::Post.new(VENDOR_CREATE_VENDOR_URI)
post_request['Content-Type'] = 'application/json'
post_request.body = JSON.dump(arguments)
print "Post Request----: #{post_request.body}\n\n"
print "-----------------------------------------------------\n"
response = JSON.parse(https.request(post_request).read_body).symbolize_keys
print "Response----: #{response}\n\n"
print "-----------------------------------------------------\n"
if response[:result] == 'success'
puts 'It was successful'
else
render json: { error: response[:message] }, status: :unprocessable_entity
end
end
# This is used once we touch the new vendor endpoint, and response with a response
def create_vendor_attributes(response)
response.slice(:action, :user_hash, :result, :vendor_uuid, :policy_shell_uuids, :refcode, :return_URL)
end
def create_vendor_params
params.require(:vendor).permit(
:request_type, :first_name, :last_name, :phone, :phone_type, :email, :address1, :address_city, :address_state, :address_zip, :DOB
)
end
def create_vendor_arguments
create_vendor_params.to_h.merge(token: CORP_RENTER_TOKEN, refcode: CORP_REFCODE)
end
end
我在这里想念什么?在我的规格上,我会发送所有内容,但我一直收到PAR JSON错误。
我会感谢帮助!
编辑
binding.pry
Create Vendor
# here it sends all the data as in the unit testing
Post Request (Unit Test)----: {"request_type":"renters","first_name":"Tony","last_name":"Stark","phone":"6504881234","phone_type":"mobile","email":"[email protected]","address1":"123 Main Street","address_city":"Palo Alto","address_state":"CA","address_zip":"94301","DOB":"1982-10-27","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
# And here it responds correctly
Response----(Unit Test): {:action=>"new_vendor", :user_hash=>"e59871c35edb7f622c97acfcc20955e9", :result=>"success", :agent_phone=>"888-888-8888", :vendor_uuid=>"242992518092902061", :policy_shell_uuids=>{"RENT"=>"243548182003445420"}, :marketing_headline=>"Test Marketing Headline", :marketing_content=>"<ul><li>Test Marketing Content</li></ul>"}
-----------------------------------------------------
creates a vendor (FAILED - 1)
# Here for some reason it only sends two parameters
Post Request----(Not Unit Test): {"last_name":"Stark","phone":"6504881234","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
# And obviously the server response with an error cause it's not sending all the required data.
Response----(Not Unit Test): {:result=>"error", :message=>"missing fields: first_name is a required field, email is a required field, address1 is a required field, address_city is a required field, address_state is a required field, address_zip is a required field, DOB is a required field, phone_type is a required field"}
创建方法
def create
vendor_new_vendor_v2
end
,该方法使用这两种方法发送参数
def create_vendor_params
params.require(:vendor).permit(
:request_type, :first_name, :last_name, :phone, :phone_type, :email, :address1, :address_city, :address_state, :address_zip, :DOB
)
end
def create_vendor_arguments
create_vendor_params.to_h.merge(token: CORP_RENTER_TOKEN, refcode: CORP_REFCODE)
end
规格
it 'creates a vendor' do
VCR.use_cassette('create_vendor') do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
response_body = JSON.parse(response.body)
expect(response_body['refcode']).to eq('corp-api-test')
end
end
#I just saw that these two `last_name`and `phone`are being printed on the puts I did above.
it 'fails if the vendor is missing attributes' do
VCR.use_cassette('create_vendor_missing_attributes') do
post '/api/v2/vendor', params: {vendor: {last_name: 'Stark', phone: '6504881234'}}.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
expect(response.status).to eq(422)
end
end
it 'requires Authorization token header' do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json'}
expect(response.status).to eq(401)
end
end
I'm quite having an issue on my Rspec. So basically what I want is to send a request to the API, and have a proper response. When I send the data to the API endpoint, and I puts
what it sends, it looks like it sends all the data correctly, but at the response level, it says that I am not sending the required parameters. (I am also using VCR to test)
My RSPEC:
require 'rails_helper'
describe 'Create Vendor', type: :request do
let(:user) { create(:alter_user) }
let(:vendor_attributes) do
{
#These are all the required parameters
vendor: {
token: '',
refcode: '',
request_type: 'renters',
first_name: 'Tony',
last_name: 'Stark',
phone: '6504881234',
phone_type: 'mobile',
email: '[email protected]',
address1: '123 Main Street',
address_city: 'Palo Alto',
address_state: 'CA',
address_zip: '94301',
DOB: '1982-10-27'
}
}
end
after do
VCR.eject_cassette
end
it 'creates a vendor' do
VCR.use_cassette('create_vendor') do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
response_body = JSON.parse(response.body)
expect(response_body['refcode']).to eq('corp-api-test')
end
end
it 'fails if the vendor is missing attributes' do
VCR.use_cassette('create_vendor_missing_attributes') do
post '/api/v2/vendor', params: {vendor: {last_name: 'Stark', phone: '6504881234'}}.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
expect(response.status).to eq(422)
end
end
it 'requires Authorization token header' do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json'}
expect(response.status).to eq(401)
end
end
When I execute the rspec filePath I receive this:
Create Vendor
Post Request----: {"request_type":"renters","first_name":"Tony","last_name":"Stark","phone":"6504881234","phone_type":"mobile","email":"[email protected]","address1":"123 Main Street","address_city":"Palo Alto","address_state":"CA","address_zip":"94301","DOB":"1982-10-27","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
Response----: {:action=>"new_vendor", :user_hash=>"e59871c35edb7f622c97acfcc20955e9", :result=>"success", :agent_phone=>"888-888-8888", :vendor_uuid=>"242992518092902061", :policy_shell_uuids=>{"RENT"=>"243548182003445420"}, :marketing_headline=>"Test Marketing Headline", :marketing_content=>"<ul><li>Test Marketing Content</li></ul>"}
-----------------------------------------------------
It was successful
creates a vendor (FAILED - 1)
Post Request----: {"last_name":"Stark","phone":"6504881234","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
Response----: {:result=>"error", :message=>"missing fields: first_name is a required field, email is a required field, address1 is a required field, address_city is a required field, address_state is a required field, address_zip is a required field, DOB is a required field, phone_type is a required field"}
-----------------------------------------------------
fails if the vendor is missing attributes
requires Authorization token header
Failures:
1) Create Vendor creates a vendor
Failure/Error: response_body = JSON.parse(response.body)
JSON::ParserError:
809: unexpected token at ''
# ./spec/requests/create_vendor_spec.rb:34:in `block (3 levels) in <top (required)>'
# ./spec/requests/create_vendor_spec.rb:32:in `block (2 levels) in <top (required)>'
# ./spec/spec_helper.rb:85:in `block (3 levels) in <top (required)>'
# ./spec/spec_helper.rb:84:in `block (2 levels) in <top (required)>'
This is my vendor controller
As you can see here I print what I send to the API, it sends the data but then it fails to create a vendor.
def corp_new_vendor_v2
arguments = create_vendor_arguments
https = Net::HTTP.new(VENDOR_CREATE_VENDOR_URI.host, VENDOR_CREATE_VENDOR_URI.port)
https.use_ssl = true
post_request = Net::HTTP::Post.new(VENDOR_CREATE_VENDOR_URI)
post_request['Content-Type'] = 'application/json'
post_request.body = JSON.dump(arguments)
print "Post Request----: #{post_request.body}\n\n"
print "-----------------------------------------------------\n"
response = JSON.parse(https.request(post_request).read_body).symbolize_keys
print "Response----: #{response}\n\n"
print "-----------------------------------------------------\n"
if response[:result] == 'success'
puts 'It was successful'
else
render json: { error: response[:message] }, status: :unprocessable_entity
end
end
# This is used once we touch the new vendor endpoint, and response with a response
def create_vendor_attributes(response)
response.slice(:action, :user_hash, :result, :vendor_uuid, :policy_shell_uuids, :refcode, :return_URL)
end
def create_vendor_params
params.require(:vendor).permit(
:request_type, :first_name, :last_name, :phone, :phone_type, :email, :address1, :address_city, :address_state, :address_zip, :DOB
)
end
def create_vendor_arguments
create_vendor_params.to_h.merge(token: CORP_RENTER_TOKEN, refcode: CORP_REFCODE)
end
end
What am I missing here? On my spec I'm sending everything but I keep receiving the par JSON error.
I will appreciate the help!
EDIT
binding.pry
Create Vendor
# here it sends all the data as in the unit testing
Post Request (Unit Test)----: {"request_type":"renters","first_name":"Tony","last_name":"Stark","phone":"6504881234","phone_type":"mobile","email":"[email protected]","address1":"123 Main Street","address_city":"Palo Alto","address_state":"CA","address_zip":"94301","DOB":"1982-10-27","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
# And here it responds correctly
Response----(Unit Test): {:action=>"new_vendor", :user_hash=>"e59871c35edb7f622c97acfcc20955e9", :result=>"success", :agent_phone=>"888-888-8888", :vendor_uuid=>"242992518092902061", :policy_shell_uuids=>{"RENT"=>"243548182003445420"}, :marketing_headline=>"Test Marketing Headline", :marketing_content=>"<ul><li>Test Marketing Content</li></ul>"}
-----------------------------------------------------
creates a vendor (FAILED - 1)
# Here for some reason it only sends two parameters
Post Request----(Not Unit Test): {"last_name":"Stark","phone":"6504881234","token":"6ed469496be882f0163c453627fbcac15b773733db123a7c2faf68f42ea7586a","refcode":"corp-api-test"}
-----------------------------------------------------
# And obviously the server response with an error cause it's not sending all the required data.
Response----(Not Unit Test): {:result=>"error", :message=>"missing fields: first_name is a required field, email is a required field, address1 is a required field, address_city is a required field, address_state is a required field, address_zip is a required field, DOB is a required field, phone_type is a required field"}
create method
def create
vendor_new_vendor_v2
end
which uses these two methods to sends the parameters
def create_vendor_params
params.require(:vendor).permit(
:request_type, :first_name, :last_name, :phone, :phone_type, :email, :address1, :address_city, :address_state, :address_zip, :DOB
)
end
def create_vendor_arguments
create_vendor_params.to_h.merge(token: CORP_RENTER_TOKEN, refcode: CORP_REFCODE)
end
specs
it 'creates a vendor' do
VCR.use_cassette('create_vendor') do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
response_body = JSON.parse(response.body)
expect(response_body['refcode']).to eq('corp-api-test')
end
end
#I just saw that these two `last_name`and `phone`are being printed on the puts I did above.
it 'fails if the vendor is missing attributes' do
VCR.use_cassette('create_vendor_missing_attributes') do
post '/api/v2/vendor', params: {vendor: {last_name: 'Stark', phone: '6504881234'}}.to_json, headers: {'Content-Type' => 'application/json', Authorization: "Token token=#{user.key}"}
expect(response.status).to eq(422)
end
end
it 'requires Authorization token header' do
post '/api/v2/vendor', params: vendor_attributes.to_json, headers: {'Content-Type' => 'application/json'}
expect(response.status).to eq(401)
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少
渲染:JSON
成功。使用以下内容更新您的控制器
You are missing the
render :json
for the success.Update your controller with the following
对于其他人,使用
jbuilder
在这里登陆相同的错误。 RSPEC默认情况下不会呈现视图,因此您需要将render_views
放在测试的顶部。For others landing here with the same error using
Jbuilder
. RSpec doesn't render views by default so you need to putrender_views
at the top of your test.