class RspecYahFormatter

Formatter that builds a pretty html report, and includes a screenshot if it is included in the example metadata.

Public Class Methods

new(out_file) click to toggle source
# File lib/rspec_yah_formatter.rb, line 19
def initialize(out_file)
  @examples = []
  @passed = 0
  @failed = 0
  @pending = 0
  if !out_file.is_a?(File)
    raise 'You should specify a html file with the --out option, STDOUT cannot be used with this formatter'
  end
  @out_file = out_file
end

Public Instance Methods

close(_notification) click to toggle source
# File lib/rspec_yah_formatter.rb, line 45
def close(_notification)
  calculate_durations
  File.open(@out_file, 'w') do |file|
    template_file = File.read(File.dirname(__FILE__) + '/../templates/report.erb')
    file.puts ERB.new(template_file).result(binding)
  end
  copy_resources
end
example_failed(notification) click to toggle source
# File lib/rspec_yah_formatter.rb, line 35
def example_failed(notification)
  @failed += 1
  @examples << Example.new(notification.example, @out_file)
end
example_passed(notification) click to toggle source
# File lib/rspec_yah_formatter.rb, line 30
def example_passed(notification)
  @passed += 1
  @examples << Example.new(notification.example, @out_file)
end
example_pending(notification) click to toggle source
# File lib/rspec_yah_formatter.rb, line 40
def example_pending(notification)
  @pending += 1
  @examples << Example.new(notification.example, @out_file)
end

Private Instance Methods

calculate_durations() click to toggle source

Calculates the total duration and an array used by jscharts with the format [[0, duration1], [1, duration2], … ]

# File lib/rspec_yah_formatter.rb, line 58
def calculate_durations
  duration_values = @examples.map(&:run_time)
  duration_values << duration_values.first if duration_values.size == 1

  @durations = duration_values.each_with_index.map { |e, i| [i, e] }
  @summary_duration = duration_values.inject(:+).to_f.to_s(:rounded, precision: 5)
end
copy_resources() click to toggle source

Copies resources to the same folder where the report will be saved

# File lib/rspec_yah_formatter.rb, line 67
def copy_resources
  FileUtils.cp_r(File.dirname(__FILE__) + '/../resources', File.dirname(@out_file))
end