2014-04-18 12 views
11

Come utilizzare Webmock per eseguire lo stub della richiesta con qualsiasi corpo e intestazione? Ho cercato di usare espressioni regolariRichiesta di stub Webmock con qualsiasi corpo e intestazione

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

in rspec ma non funziona, ha

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

risposta

3

Scopri i Webmock docs on headers. Devi fornire un hash con ulteriori dettagli su ciò che stai cercando.

+0

penso che questa sezione è un riferimento migliore per come 'headers' vengono confrontati: https://github.com/ bblimke/webmock # matching-header – Aegix

8

Dovreste essere in grado di utilizzare solo

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

Se non si specifica del corpo o le intestazioni sulla matrice stessa, consentirà nulla per il corpo o le intestazioni. Questo non si applica ai parametri di query.

Ad esempio, il test di questo progetto campione passa:

bundle exec rspec uscita:

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-ver sione

ruby-2.0.0 

Gemfile

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 
+2

Questo non è vero, non corrisponderà. – lzap

+1

Sì. Dovrebbe funzionare. Pensavo che non fosse così, ma poi mi sono reso conto che in realtà non avevo mai chiamato 'stub_request' –

+0

FARE ATTENZIONE ALL'URL TRAILING sull'URL! stub_request (: get, 'h ttp: //www.example.com') corrisponderà a qualsiasi cosa, ma stub_request (: get, 'h ttp: //www.example.com/') non lo farà !! Ho perso un po 'di tempo su questo. –

Problemi correlati