【问题标题】:Passing Two Parameters on a JSON POST Request in Rspec在 Rspec 中传递 JSON POST 请求的两个参数
【发布时间】: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


    【解决方案1】:

    您正在寻找的是Hash.merge(other_hash)。您可能想阅读 Hash 类,因为大约 50% 的 Ruby 编程是关于哈希操作(恕我直言)。

    这是您在规范中使用它的方式:

    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
    

    【讨论】:

    • 50% 的 Ruby 编程是关于散列操作的 - 我认为只有 10% 是散列操作,另外 10% 正在弄清楚如何在其中编写方法行,80% 的人在吹嘘 Ruby 有多棒。 :)
    • 谢谢,maxcal。我最终需要另一个答案的一个班轮。我已经在使用 FactoryGirl,但测试的重点是 API 是否创建记录。我会支持那 80%,zetetic!
    • 那你就可以FactoryGirl.attributes_for(:product, organization_type: "ABC")
    【解决方案2】:

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

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

    【讨论】:

    • 最简单的答案获胜!谢谢你,加布里埃尔希拉尔。
    猜你喜欢
    • 2016-01-25
    • 1970-01-01
    • 2016-11-05
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多