在Rspec中的JSON POST请求上传递两个参数

时间:2015-06-19 19:50:03

标签: ruby-on-rails ruby json post rspec

我有一个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

但请求会忽略第二个参数(我可以切换它们的位置以确认)。如何填写帖子,以便读入两个参数?

2 个答案:

答案 0 :(得分:1)

我认为您应该在帖子中发送一个哈希值。请尝试以下方法:

post :create, json.merge!(access_token: @api_app.access_token)

答案 1 :(得分:1)

您要找的是Hash.merge(other_hash)。您可能希望对Hash类进行一些阅读,因为大约50%的Ruby编程是关于散列操作(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响应代码 - 但如果缺少参数,则应返回a 400 Bad Request。正确的期望是:

expect(response).to have_http_status :bad_request