2014-02-28 72 views
1

在請求example.com的所有規範中,我希望忽略URI中與請求匹配器相關的結尾ID。像這樣的東西。根據請求在VCR中設置默認選項URI

VCR.configure do |c| 
    # omitted 
    c.register_request_matcher :uri_ignoring_trailing_id do |request_1, request_2| 
    # omitted 
    end 

    c.before_http_request(lambda { |req| req.uri =~ /example.com/ }) do 
    c.default_cassette_options = { match_requests_on: [ :uri_ignoring_trailing_id ] } 
    end 

end 

回答

5

修改before_http_request全局配置是一個糟糕的主意,因爲它會影響要求而作出更改配置後,不只是那些匹配example.com。以下是我建議你這樣做的建議:

VCR.configure do |vcr| 
    uri_matcher = VCR.request_matchers[:uri] 
    vcr.register_request_matcher(:uri_ignoring_trailing_id_for_example_dot_com) do |req_1, req_2| 
    if req_1.parsed_uri.host == "example.com" && req_2.parsed_uri.host == "example.com" 
     # do your custom matching where you ignore trailing id 
    else 
     uri_matcher.matches?(req_1, req_2) 
    end 
    end 

    vcr.default_cassette_options = { match_requests_on: [:method, :uri_ignoring_trailing_id_for_example_dot_com] } 
end