代码之家  ›  专栏  ›  技术社区  ›  Steve Redka

如何使用嵌套数据编写功能强大的Webmock存根?

  •  0
  • Steve Redka  · 技术社区  · 7 年前

    其中一些调用发送嵌套数据,而Webmock似乎从未正确处理这些数据。

    describe 'calc' do
      before do
        stub_request(:any, url).with(body: hash_including(operation: 'calc'))
      end
    
      it 'works' do
        request_data = { sale_type: 'money', cover_type: 'money', 
                         region: 'rf', period: 9,
                         adults: 600, children: 750, mice: 500 }
    
        # This thing makes a HTTP request:
        MiteService.new(login_params).calc(request_data)
    
        expected_body = { operation: 'calc', product: 'mite3', 
                          sessionID: session, data: request_data }
        expect(WebMock).to have_requested(:post, url).with(body: expected_body)
      end
    end
    

    data

    1) MiteService API calls calc works
     Failure/Error: expect(WebMock).to have_requested(:post, url).with(body: expected_body)
    
       The request POST http://example.com/api with body {"data"=>{"sale_type"=>"money", "cover_type"=>"money", "region"=>"rf", "period"=>9, "adults"=>600, "children"=>750, "mice"=>500}, "operation"=>"calc", "product"=>"mite3", "sessionID"=>"123"} was expected to execute 1 time but it executed 0 times
    
       The following requests were made:
    
       <...>
       POST http://example.com/api with body 'operation=calc&product=mite3&sessionID=123&data=%7B%3Asale_type%3D%3E%22money%22%2C+%3Acover_type%3D%3E%22money%22%2C+%3Aregion%3D%3E%22rf%22%2C+%3Aperiod%3D%3E9%2C+%3Aadults%3D%3E600%2C+%3Achildren%3D%3E750%2C+%3Amice%3D%3E500%7D' with headers {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded', 'Date'=>'Tue, 25 Dec 2018 08:20:32 GMT', 'User-Agent'=>'HTTPClient/1.0 (2.8.3, ruby 2.4.1 (2017-03-22))'} was made 1 time
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Steve Redka    7 年前

    expect(WebMock).to have_requested(:post, url).with(body: expected_body.to_json)
    
    推荐文章