Class: Soaspec::Baseline
- Inherits:
-
Object
- Object
- Soaspec::Baseline
- Includes:
- ExeHelpers
- Defined in:
- lib/soaspec/baseline.rb
Overview
Used for defining parameters for recording and asserting against a baseline
Constant Summary collapse
- ALLOWED_FORMATS =
Returns List of allowed formats.
[:raw, :hash]
Class Attribute Summary collapse
-
.folder ⇒ String
Folder where baselines are recorded and retrieved.
Instance Attribute Summary collapse
-
#description ⇒ String
Name of file including folders describing baseline.
-
#exchange ⇒ Exchange
Exchange object to save/assert baseline for.
-
#format ⇒ Symbol
Format in which baseline is stored.
Instance Method Summary collapse
-
#actual_content ⇒ String, Hash
Actual response from API.
-
#content_to_save ⇒ Object
Content to save as a baseline.
-
#file ⇒ String
File where baseline is stored.
-
#initialize(exchange, format) ⇒ Baseline
constructor
A new instance of Baseline.
-
#matches? ⇒ Boolean
Compare baseline with expected result.
-
#read_baseline ⇒ String
Result of reading baseline.
Methods included from ExeHelpers
#class_content, #create_file, #create_files, #create_files_for, #create_folder, #generated_soap_spec_for, #retrieve_contents, #spec_task
Constructor Details
#initialize(exchange, format) ⇒ Baseline
Returns a new instance of Baseline.
22 23 24 25 26 27 28 29 |
# File 'lib/soaspec/baseline.rb', line 22 def initialize(exchange, format) self.exchange = exchange self.format = format unless ALLOWED_FORMATS.include? format raise ArgumentError, "Expected format #{format} to be " \ "either #{ALLOWED_FORMATS}" end end |
Class Attribute Details
.folder ⇒ String
Returns Folder where baselines are recorded and retrieved.
80 81 82 |
# File 'lib/soaspec/baseline.rb', line 80 def folder @folder end |
Instance Attribute Details
#description ⇒ String
Returns Name of file including folders describing baseline.
74 75 76 |
# File 'lib/soaspec/baseline.rb', line 74 def description @description || exchange.request_parameters.description end |
#exchange ⇒ Exchange
Returns Exchange object to save/assert baseline for.
14 15 16 |
# File 'lib/soaspec/baseline.rb', line 14 def exchange @exchange end |
#format ⇒ Symbol
Returns Format in which baseline is stored. Either :raw or :hash.
16 17 18 |
# File 'lib/soaspec/baseline.rb', line 16 def format @format end |
Instance Method Details
#actual_content ⇒ String, Hash
Returns Actual response from API.
51 52 53 54 55 56 57 58 59 |
# File 'lib/soaspec/baseline.rb', line 51 def actual_content if format == :hash exchange.to_hash elsif format == :raw exchange.pretty_response_body.strip else raise NotImplementedError, "Format #{format} is not #{ALLOWED_FORMATS}" end end |
#content_to_save ⇒ Object
Content to save as a baseline
46 47 48 |
# File 'lib/soaspec/baseline.rb', line 46 def content_to_save format == :raw ? exchange.pretty_response_body : YAML.dump(exchange.to_hash) end |
#file ⇒ String
Returns File where baseline is stored.
67 68 69 70 71 |
# File 'lib/soaspec/baseline.rb', line 67 def file File.join(self.class.folder, exchange.exchange_handler.to_s.snakecase, "#{description}.#{format == :raw ? exchange.format : 'yml'}") end |
#matches? ⇒ Boolean
Compare baseline with expected result. This will create baseline if not created
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/soaspec/baseline.rb', line 34 def matches? if File.exist?(file) actual_content == read_baseline else create_file filename: file, content: content_to_save raise Soaspec::BaselineError, "Created baseline at #{file}. Inspect file to ensure it is correct and rerun to ensure baseline is stable" end end |
#read_baseline ⇒ String
Returns Result of reading baseline.
62 63 64 |
# File 'lib/soaspec/baseline.rb', line 62 def read_baseline format == :raw ? File.read(file).strip : YAML.load_file(file) end |