2014-04-18 10 views
11

Webmock, herhangi bir gövde ve üstbilgiyle stub isteği için nasıl kullanılır? Ben Regex RSpec içindeHerhangi bir gövde ve üstbilgiyle Webmock saplama isteği

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

kullanmaya çalıştı ama çalışmıyor, bu

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

cevap

3

Kontrol dışarı Webmock docs on headers sahiptir. Eşleştirdiğiniz şeyle ilgili daha fazla ayrıntıya sahip bir karma sağlamanız gerekir.

+0

bu bölümde 'headers' karşı nasıl eşleştiği için daha iyi bir referans olduğunu düşünüyorum bblimke/webmock # matching-header – Aegix

8

Sen saplama kendisinde vücudu veya başlıkları belirtmezseniz sadece

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

kullanmak gerekir, vücut ya da başlıkları için her şeyi sağlayacaktır. Bu, sorgu parametreleri için geçerli değildir.

Örneğin, bu örnek projenin testi geçer:

bundle exec rspec çıkışı:

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 

spektrumu/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 https://github.com/: sion

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

Bu doğru değil, eşleşmeyecek. – lzap

+1

Evet. İşe yaramalı. Ben de bilmiyordum, ama daha sonra asla url üzerinde 'stub_request' –

+0

İYİLEŞTİRME SLASH İMALATI adını hiç fark etmemişti! stub_request (: get, 'h ttp: //www.example.com') bir şeyle eşleşir, ancak stub_request (: get, 'h ttp: //www.example.com/') olmaz! Bunun için biraz zaman kaybettim. –

İlgili konular