【发布时间】:2017-09-11 18:31:09
【问题描述】:
为了通过考试,我一直在扯头发。我有一个如下所示的 JSON API:
{
"data": [
{
"id": "b99f8173-0492-457f-9de9-6c1d8d6832ed",
"type": "manufacturer_organizations",
"attributes": {
"account_number": "random test 123"
},
"relationships": {
"organization": {
"data": {
"id": "fb20ddc9-a3ee-47c3-bdd2-f710541ff89c",
"type": "organizations"
}
},
"manufacturer": {
"data": {
"id": "1",
"type": "manufacturers"
}
}
}
},...
我正在尝试在 Rails 中进行post :create 测试。
let!(:manufacturer_organization) {FactoryGirl.create(:manufacturer_organization)}
let(:manufacturer_organization2) { FactoryGirl.create(:manufacturer_organization)}
...
describe "POST create" do
it "posts a valid manufacturer organization data" do
authenticate
organization = FactoryGirl.create(:organization)
manufacturer = FactoryGirl.create(:manufacturer)
post :create, manufacturer_organization2.to_json #what should I put here instead?
expect(json['data'].length).to eq(2)
end
#=> error: JSON::ParserError: A JSON text must at least contain two octets!
我尝试过各种 SO 帖子(this)、this 和 this article
以下是我尝试过的一些尝试:
post :create, params: {organization_id: organization.id, manufacturer: manufacturer.id, account_number: "123 test account number"}
#=> error: JSON::ParserError:
A JSON text must at least contain two octets!
或
post :create, params: :manufacturer_organization_2
#=>
NoMethodError:
undefined method `symbolize_keys' for :manufacturer_organization_2:Symbol
或
json = { :format => 'json', :manufacturer_organization => { :account_number => "foo123", :organization_id => organization.id, :manufacturer_id => manufacturer.id } }
post :create, json
#=> NoMethodError:
undefined method `length' for nil:NilClass
如何测试我的控制器通过post :create 接受manufacturer_id, organization_id, and account_number?现在我测试它的方式是计算初始json['data'].length(初始为1);最后,我希望 json['data'].length 在成功 post :create 后为 2。如何模拟创建有效的manufacturer_organization 输入?
编辑:
抱歉,忘记放我的json方法助手了:
def json
JSON.parse(response.body)
end
另外,这个pass:
describe "POST create" do
it "posts a valid manufacturer organization data" do
authenticate
organization = FactoryGirl.create(:organization)
manufacturer = FactoryGirl.create(:manufacturer)
post :create, {account_number: "Test account numba", organization_id: organization.id, manufacturer_id: manufacturer.id}
expect(response).to be_success
end
添加expect(json['success']).to eq("Yay!") 时出现此错误:
JSON::ParserError:
A JSON text must at least contain two octets!
控制器:
def create
@manufacturer_organization = ManufacturerOrganization.new(manufacturer_organization_params)
if @manufacturer_organization.save
puts "success!"
render json: {success: "Yay!"}
else
puts "Sorry, something went wrong!"
end
end
def manufacturer_organization_params
api_params.permit(:organization_id, :manufacturer_id, :account_number)
end
而@api_params ||= ActionController::Parameters.new(ActiveModelSerializers::Deserialization.jsonapi_parse(params))
【问题讨论】:
-
您不需要在规范中明确地将参数转换为 JSON。但是这里的代码中还有很多其他问题。您的示例 JSON 中的有效负载类似于 JSONAPI.org。但它不是创建资源的正确有效负载。此外,您正在使用
FactoryGirl.create,这会给您带来唯一性验证问题。而不是在您的控制器中使用puts,而是返回有意义的 HTTP 响应代码并在您的规范中进行测试。 -
您应该编辑问题并说明您的控制器接受什么以及您尝试测试的结果是什么。这样,回答者就可以给出实际有效的示例,而不是用代码修复 10 个问题中的 1 个。
-
感谢您的评论,@max!您介意详细说明如何返回有意义的 HTTP 响应代码并在我的规范中进行测试吗?我还编辑了问题并为我的参数添加了更多代码。
标签: ruby-on-rails rspec json-api