Ruby on rails 在Rspec中对JSON POST请求传递两个参数
我有一个JSON Rails 4 API,我正在用Rspec进行测试。我在传递帖子中的两个参数时遇到问题:createrequest 以下是当前的测试:Ruby on rails 在Rspec中对JSON POST请求传递两个参数,ruby-on-rails,ruby,json,post,rspec,Ruby On Rails,Ruby,Json,Post,Rspec,我有一个JSON Rails 4 API,我正在用Rspec进行测试。我在传递帖子中的两个参数时遇到问题:createrequest 以下是当前的测试: require 'spec_helper' module Api module V1 describe Api::V1::ProductsController, type: :controller do before do @api_app = FactoryGirl.create
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_令牌:
post :create, json, access_token: @api_app.access_token
但是请求忽略了第二个参数(我可以切换它们的位置以确认)。我如何给帖子添加单词,以便两个参数都被读入?我认为你应该在帖子中发送一个散列。请尝试以下操作:
post :create, json.merge!(access_token: @api_app.access_token)
我认为你应该在你的帖子里发一个散列。请尝试以下操作:
post :create, json.merge!(access_token: @api_app.access_token)
我认为你应该在你的帖子里发一个散列。请尝试以下操作:
post :create, json.merge!(access_token: @api_app.access_token)
我认为你应该在你的帖子里发一个散列。请尝试以下操作:
post :create, json.merge!(access_token: @api_app.access_token)
你要找的是。您可能想阅读一下Hash类,因为大约50%的Ruby编程都是关于Hash操作(IMHO)的 以下是在规范中使用它的方式:
require 'spec_helper'
# You don't need to put the spec inside your module!
# It just makes it harder to read!
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
# DRY up the parameters
let(:params){
access_token: @api_app.access_token
format: 'json'
}
# Even better is to use fixures or factories for this.
let(:product_params) {
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"
}
context "Creates a product with the correct parameters" do
it "returns a successful response string with success message" do
product = product_params.merge(organization_type: "Org")
post :create, params.merge(product: product)
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
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success
expect(response.body).to include("Product failed to create")
end
end
end
end
但是,您的规范包含错误:
context "Incorrect parameters to create a product" do
it "returns a failure response when a parameter is missing" do
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success #! it should not be a success!
end
end
expect(response).to be_success
实际上意味着响应应该具有200 OK HTTP响应代码-但是如果缺少参数,则应该返回。正确的期望是:
expect(response).to have_http_status :bad_request
你要找的是。您可能想阅读一下Hash类,因为大约50%的Ruby编程都是关于Hash操作(IMHO)的 以下是在规范中使用它的方式:
require 'spec_helper'
# You don't need to put the spec inside your module!
# It just makes it harder to read!
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
# DRY up the parameters
let(:params){
access_token: @api_app.access_token
format: 'json'
}
# Even better is to use fixures or factories for this.
let(:product_params) {
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"
}
context "Creates a product with the correct parameters" do
it "returns a successful response string with success message" do
product = product_params.merge(organization_type: "Org")
post :create, params.merge(product: product)
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
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success
expect(response.body).to include("Product failed to create")
end
end
end
end
但是,您的规范包含错误:
context "Incorrect parameters to create a product" do
it "returns a failure response when a parameter is missing" do
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success #! it should not be a success!
end
end
expect(response).to be_success
实际上意味着响应应该具有200 OK HTTP响应代码-但是如果缺少参数,则应该返回。正确的期望是:
expect(response).to have_http_status :bad_request
你要找的是。您可能想阅读一下Hash类,因为大约50%的Ruby编程都是关于Hash操作(IMHO)的 以下是在规范中使用它的方式:
require 'spec_helper'
# You don't need to put the spec inside your module!
# It just makes it harder to read!
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
# DRY up the parameters
let(:params){
access_token: @api_app.access_token
format: 'json'
}
# Even better is to use fixures or factories for this.
let(:product_params) {
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"
}
context "Creates a product with the correct parameters" do
it "returns a successful response string with success message" do
product = product_params.merge(organization_type: "Org")
post :create, params.merge(product: product)
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
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success
expect(response.body).to include("Product failed to create")
end
end
end
end
但是,您的规范包含错误:
context "Incorrect parameters to create a product" do
it "returns a failure response when a parameter is missing" do
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success #! it should not be a success!
end
end
expect(response).to be_success
实际上意味着响应应该具有200 OK HTTP响应代码-但是如果缺少参数,则应该返回。正确的期望是:
expect(response).to have_http_status :bad_request
你要找的是。您可能想阅读一下Hash类,因为大约50%的Ruby编程都是关于Hash操作(IMHO)的 以下是在规范中使用它的方式:
require 'spec_helper'
# You don't need to put the spec inside your module!
# It just makes it harder to read!
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
# DRY up the parameters
let(:params){
access_token: @api_app.access_token
format: 'json'
}
# Even better is to use fixures or factories for this.
let(:product_params) {
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"
}
context "Creates a product with the correct parameters" do
it "returns a successful response string with success message" do
product = product_params.merge(organization_type: "Org")
post :create, params.merge(product: product)
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
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success
expect(response.body).to include("Product failed to create")
end
end
end
end
但是,您的规范包含错误:
context "Incorrect parameters to create a product" do
it "returns a failure response when a parameter is missing" do
product = product_params.merge(organization_type: "ABC")
post :create, params.merge(product: product)
expect(response).to be_success #! it should not be a success!
end
end
expect(response).to be_success
实际上意味着响应应该具有200 OK HTTP响应代码-但是如果缺少参数,则应该返回。正确的期望是:
expect(response).to have_http_status :bad_request
50%的Ruby编程是关于散列操作的——我认为只有10%是散列操作,另外10%是在一行上计算如何编写方法,80%是吹嘘Ruby有多棒。:)谢谢,麦克斯卡尔。我最终需要另一个答案中的一句话。我已经在使用FactoryGirl了,但是测试的重点是API是否创建了记录。我会支持80%,泽蒂!然后你可以为(:产品,组织,类型:“ABC”)做
FactoryGirl.attributes\u
50%的Ruby编程是关于散列操作的——我认为只有10%是散列操作,另外10%是在一行计算如何编写你的方法,80%是在吹嘘Ruby有多棒。:)谢谢,麦克斯卡尔。我最终需要另一个答案中的一句话。我已经在使用FactoryGirl了,但是测试的重点是API是否创建了记录。我会支持80%,泽蒂!然后你可以为(:产品,组织,类型:“ABC”)做FactoryGirl.attributes\u
50%的Ruby编程是关于散列操作的——我认为只有10%是散列操作,另外10%是在一行计算如何编写你的方法,80%是在吹嘘Ruby有多棒。:)谢谢,麦克斯卡尔。我最终需要另一个答案中的一句话。我已经在使用FactoryGirl了,但是测试的重点是API是否创建了记录。我会支持80%,泽蒂!然后你可以为(:产品,组织,类型:“ABC”)做FactoryGirl.attributes\u
50%的Ruby编程是关于散列操作的——我认为只有10%是散列操作,另外10%是在一行计算如何编写你的方法,80%是在吹嘘Ruby有多棒。:)谢谢,麦克斯卡尔。我最终需要另一个答案中的一句话。我已经在使用FactoryGirl了,但是测试的重点是API是否创建了记录。我会支持80%,泽蒂!然后你可以为(:产品,组织,键入:“ABC”)做FactoryGirl.attributes\u
最简单的答案就赢了!谢谢你,加布里埃尔·希拉尔。最简单的答案就是胜利!谢谢你,加布里埃尔·希拉尔。最简单的答案就是胜利!谢谢你,加布里埃尔·希拉尔。最简单的答案就是胜利!谢谢你,加布里埃尔·希拉尔。