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 do |response|
  code = "    response = Typhoeus::Response.new(\n      \#{response.options.except(: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