File: webmock_helper.rb

package info (click to toggle)
ruby-json-jwt 1.16.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: ruby: 2,858; makefile: 4
file content (34 lines) | stat: -rw-r--r-- 923 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'webmock/rspec'

module WebMockHelper
  def mock_json(method, endpoint, response_file, options = {})
    stub_request(method, endpoint).to_return(
      response_for(response_file, options)
    )
    result = yield
    a_request(method, endpoint).should have_been_made.once
    result
  end

  def request_to(endpoint, method = :get)
    raise_error { |e|
      e.should be_instance_of WebMock::NetConnectNotAllowedError
      e.message.should include("Unregistered request: #{method.to_s.upcase}")
      e.message.should include(endpoint)
    }
  end

  private

  def response_for(response_file, options = {})
    response = {}
    response[:body] = File.new(File.join(File.dirname(__FILE__), '../mock_response', "#{response_file}.#{options[:format] || :json}"))
    if options[:status]
      response[:status] = options[:status]
    end
    response
  end
end

include WebMockHelper
WebMock.disable_net_connect!