【问题标题】:Webmock stub request with any body and header带有任何正文和标头的 Webmock 存根请求
【发布时间】:2014-06-03 06:51:47
【问题描述】:

如何使用 Webmock 来存根带有任何正文和标头的请求?我尝试使用正则表达式

WebMock.stub_request(:post, 'api.quickblox.com/').with(:body => /.*?/, :headers => /.*?/).to_return(:status => 201, :body => "", :headers => {})

在 rspec 但它不起作用,它有

NoMethodError:
   undefined method `map' for /.*?/:Regexp

【问题讨论】:

    标签: ruby-on-rails rspec webmock


    【解决方案1】:

    你应该可以使用

    WebMock.stub_request(:post, 'api.quickblox.com/')
      .to_return(status: 201, body: 'whatever', headers: { some_kind_of: 'header' })
    

    如果您没有在存根本身上指定正文或标题,它将允许正文或标题的任何内容。这不适用于查询参数。

    例如,这个示例项目的测试通过:

    bundle exec rspec 输出:

    Test
      does a thing
    
    Finished in 0.00379 seconds (files took 0.25797 seconds to load)
    1 example, 0 failures
    

    lib/test.rb

    require 'faraday'
    require 'json'
    
    class Test
      def self.do_a_thing
        JSON.parse(Faraday.get('http://www.example.com') do |request|
          request.headers['Something'] = 'anything'
          request.body = 'bla bla bla'
        end.body)
      end
    end
    

    spec/test_spec.rb

    require_relative '../lib/test.rb'
    require 'webmock/rspec'
    
    describe Test do
      WebMock.stub_request(:get, 'http://www.example.com')
        .to_return(body: '{ "this": "is a test" }')
    
      it 'does a thing' do
        expect(described_class.do_a_thing).to include({ 'this' => 'is a test' })
      end
    end
    

    .ruby 版本

    ruby-2.0.0
    

    宝石文件

    gem 'rspec'
    gem 'webmock'
    gem 'faraday'
    

    Gemfile.lock

    GEM
      specs:
        addressable (2.3.8)
        crack (0.4.2)
          safe_yaml (~> 1.0.0)
        diff-lcs (1.2.5)
        faraday (0.9.1)
          multipart-post (>= 1.2, < 3)
        multipart-post (2.0.0)
        rspec (3.2.0)
          rspec-core (~> 3.2.0)
          rspec-expectations (~> 3.2.0)
          rspec-mocks (~> 3.2.0)
        rspec-core (3.2.3)
          rspec-support (~> 3.2.0)
        rspec-expectations (3.2.1)
          diff-lcs (>= 1.2.0, < 2.0)
          rspec-support (~> 3.2.0)
        rspec-mocks (3.2.1)
          diff-lcs (>= 1.2.0, < 2.0)
          rspec-support (~> 3.2.0)
        rspec-support (3.2.2)
        safe_yaml (1.0.4)
        webmock (1.20.4)
          addressable (>= 2.3.6)
          crack (>= 0.3.2)
    
    PLATFORMS
      ruby
    
    DEPENDENCIES
      faraday
      rspec
      webmock
    
    BUNDLED WITH
       1.10.5
    

    【讨论】:

    • 这不是真的,它不会匹配。
    • 是的。它应该工作。我也以为没有,但后来我意识到我实际上从来没有打电话给stub_request
    • 当心 url 上的尾部斜线! stub_request(:get, 'h ttp://www.example.com') 将匹配任何内容,但 stub_request(:get, 'h ttp://www.example.com/') 不会!在这方面浪费了一些时间。
    • 确保网址完全匹配。如果您没有在期望中指定协议,它将默认为http
    【解决方案2】:

    查看Webmock docs on headers。您需要为它提供一个哈希,其中包含有关您匹配的内容的更多详细信息。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2015-04-29
    • 1970-01-01
    • 2018-12-06
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多