Class: Automock::ResponseMock

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

Instance Method Summary collapse

Constructor Details

#initialize(context, example) ⇒ ResponseMock

Returns a new instance of ResponseMock.



5
6
7
8
# File 'lib/automock/response_mock.rb', line 5

def initialize(context, example)
  @context = context
  @example = example
end

Instance Method Details

#descriptionObject



10
11
12
# File 'lib/automock/response_mock.rb', line 10

def description
  @example.try(:example_group).try(:description) || @example.description
end

#filenameObject



34
35
36
# File 'lib/automock/response_mock.rb', line 34

def filename
  "#{method}_#{description.gsub(/\s/, '_').gsub(/[?"\\\<>*|]/, '')}.json"
end

#methodObject



14
15
16
# File 'lib/automock/response_mock.rb', line 14

def method
  @context.request.method
end

#mock_dataObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/automock/response_mock.rb', line 38

def mock_data
  {
    description: description,
    method: method,
    uri: uri,
    status: status,
    response_header: response_header,
    response_body: response_body
  }.to_json
end

#response_bodyObject



30
31
32
# File 'lib/automock/response_mock.rb', line 30

def response_body
  @context.response.try(:body)
end

#response_headerObject



22
23
24
# File 'lib/automock/response_mock.rb', line 22

def response_header
  @context.response.try(:header)
end

#statusObject



26
27
28
# File 'lib/automock/response_mock.rb', line 26

def status
  @context.response.try(:status)
end

#uriObject



18
19
20
# File 'lib/automock/response_mock.rb', line 18

def uri
  @context.request.env['PATH_INFO']
end

#writeObject



49
50
51
52
53
# File 'lib/automock/response_mock.rb', line 49

def write
  pathname = Pathname.new("#{Rails.root}/automock/data/#{uri}/#{filename}")
  pathname.parent.mkpath
  pathname.open('w') { |file| file << mock_data }
end