Module: CapybaraMock

Defined in:
lib/capybara_mock.rb,
lib/capybara_mock/stub.rb,
lib/capybara_mock/version.rb,
lib/capybara_mock/interceptor/base.rb,
lib/capybara_mock/interceptor/cuprite.rb

Overview

## CapybaraMock

Mock capybara browser http requests.

Examples:

Stub external requests using ‘stub_request`


CapybaraMock
  .stub_request(:get, 'http://example.com')
  .to_return(status: 200, headers: {}, body: 'Hello')

Stub internal requests using ‘stub_path“


CapybaraMock
  .stub_path(:get, '/api/user/101')
  .to_return(status: 200, headers: {}, body: 'Hello')

More strict stub


CapybaraMock
  .stub_request(:get, 'http://example.com')
  .with(
    query: {
      token: '12345678'
    },
    headers: {
      'Authorization' => 'Bearer 12345678',
      'Content-Type' => 'application/json'
    },
    body: {
      message: 'ping'
    }.to_json
  ).to_return(
    status: 200,
    headers: {
      'Content-Type' => 'application/json'
    },
    body: {
      message: 'pong'
    }.to_json
  )

Dynamic stub based on actual request


CapybaraMock.stub_request(:get, /example/).to_return do |query:, **|
  state = query['state']
  [200, {'Location' => "http://localhost/foo?state={state}"}, '']
end

Defined Under Namespace

Modules: Interceptor Classes: Error, Stub

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.clear_stubsObject

Clear all current session stubs



89
90
91
# File 'lib/capybara_mock.rb', line 89

def clear_stubs
  interceptor.stubs.clear
end

.remove_stub(stub) ⇒ Object

Remove capybara stub from current session.

Parameters:



84
85
86
# File 'lib/capybara_mock.rb', line 84

def remove_stub(stub)
  interceptor.stubs.delete(stub)
end

.reset!Object

Reset capybara mock interceptor.



112
113
114
# File 'lib/capybara_mock.rb', line 112

def reset!
  @interceptor = nil
end

.save_unstubbed_requests(path) ⇒ Object

Save unstubbed requests to path.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/capybara_mock.rb', line 94

def save_unstubbed_requests(path)
  return false unless @interceptor && @interceptor.unstubbed_requests.present?

  File.open(path, 'w') do |f|
    @interceptor.unstubbed_requests.each do |request|
      f.puts "#{request[:method]} #{request[:url]} #{request[:query]}"
      request[:headers].each do |k, v|
        f.puts "#{k}: #{v}"
      end
      f.puts request[:body]
      f.puts
    end
  end

  true
end

.stub_path(method, path, &block) ⇒ CapybaraMock::Stub

Stub capybara request for current session using path.

Parameters:

  • method (String)
  • path (String)

Returns:



64
65
66
67
# File 'lib/capybara_mock.rb', line 64

def stub_path(method, path, &block)
  url = File.join(capybara_session.server.base_url, path)
  stub_request(method, url, &block)
end

.stub_request(method, url, &block) ⇒ CapybaraMock::Stub

Stub capybara request for current session using url. It also initialize interceptor for current session on first stub.

Parameters:

  • method (String)
  • url (String)

Returns:



75
76
77
78
79
# File 'lib/capybara_mock.rb', line 75

def stub_request(method, url, &block)
  Stub.new(method, url, &block).tap do |stub|
    interceptor.stubs << stub
  end
end