Class: MiniMock

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_mock.rb

Constant Summary collapse

RECORDINGS_PATH =
'mini_mock'
DEFAULT_FILE =
"mocks"
AFTER_REQUEST_CALLBACK =
Proc.new do |response|
  code = "    response = Typhoeus::Response.new(\n      code: \#{response.code},\n      status_message: \"\#{response.status_message}\",\n      body: \#{response.body.inspect},\n      headers: \#{response.headers},\n      effective_url: \#{response.effective_url.inspect},\n      options: \#{response.options.tap{|a| a[:debug_info] = \"\"}.inspect}\n    )\n    Typhoeus.stub(\#{response.request.base_url.inspect}, \#{response.request.original_options})\n    .and_return(response)\n\n  RUBY\n  File.write \"\#{RECORDINGS_PATH}/\#{@@file_name}.rb\", code, mode: 'a+'\nend\n"

Class Method Summary collapse

Class Method Details

.offObject



22
23
24
25
26
27
# File 'lib/mini_mock.rb', line 22

def off
  Typhoeus::Config.block_connection = false
  Typhoeus::Expectation.clear
  remove_callback
  true
end

.record(file_name = DEFAULT_FILE) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mini_mock.rb', line 7

def record file_name=DEFAULT_FILE
  FileUtils.mkdir_p(RECORDINGS_PATH)
  @@file_name = file_name
  Typhoeus::Config.block_connection = false
  add_callback
  true
end

.replay(file_name = DEFAULT_FILE) ⇒ Object



15
16
17
18
19
20
# File 'lib/mini_mock.rb', line 15

def replay file_name=DEFAULT_FILE
  Typhoeus::Config.block_connection = true
  remove_callback
  load "#{RECORDINGS_PATH}/#{file_name}.rb"
  true
end