【发布时间】:2015-06-19 19:50:02
【问题描述】:
我有一个 JSON Rails 4 API,我正在使用 Rspec 进行测试。我在传递给帖子中的两个参数时遇到了麻烦:创建请求。
这是当前的测试:
require 'spec_helper'
module Api
module V1
describe Api::V1::ProductsController, type: :controller do
before do
@api_app = FactoryGirl.create(:api_app, request_origin: "0.0.0.0")
@store = FactoryGirl.create(:store)
end
after(:all) do
Product.all.each {|l| l.destroy}
ApiApp.all.each {|a| a.destroy}
end
describe "POST 'create' " do
context "Creates a product with the correct parameters" do
it "returns a successful response string with success message" do
json = { :format => 'json', product:{first_name:"Hello", last_name:"You", email:"email@email.edu",street1:"103 ABC Street", city:"Pittsburgh", phone:"4125361111", store_id:@store.id, state:"CA", country:"United States", organization:"ABC", organization_type: "Org"}}
post :create, json, access_token: @api_app.access_token
expect(response).to be_success
expect(response.body).to include("Product created")
end
end
context "Incorrect parameters to create a product" do
it "returns a failure response when a parameter is missing" do
json = { :format => 'json', product:{first_name:"Hello", last_name:"You", email:"email@email.edu",street1:"103 ABC Street", city:"Pittsburgh", phone:"4125361111", store_id:@store.id, state:"CA", country:"United States", organization:"ABC"}}
post :create, json, access_token: @api_app.access_token
expect(response).to be_success
expect(response.body).to include("Product failed to create")
end
end
end
end
end
end
我需要json和access_token就行了:
post :create, json, access_token: @api_app.access_token
但是请求忽略了第二个参数(我可以切换它们的位置来确认)。我如何对帖子进行措辞以便读取两个参数?
【问题讨论】:
标签: ruby-on-rails ruby json post rspec