Sha256: 2b15c85008ccd8ac1931e8201f56950fd61609350c7a19eb201b2c2b000afb5e

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

require "spec/runner/formatter/base_formatter"

class TestingFormatter < Spec::Runner::Formatter::BaseFormatter
  def initialize(options, output, example_state)
    @example_state = example_state
  end

  def example_failed(example_proxy, counter, failure)
    @example_state.failed!(failure)
  end
end

class ExampleState
  attr_reader :exception
  def failed?
    !exception.nil?
  end
  def failed!(failure)
    @exception = failure.exception
  end
end

class BeSuccessful
  def matches?(example)
    @example = example
    !example.failed?
  end

  def description
    "be successful"
  end

  def failure_message_for_should
    "expected example to be successful, but it failed with:\n" +
    "  Message: #{@example.exception.message}\n" +
    "  Backtrace:\n" +
    "    #{@example.exception.backtrace.join("\n    ")}"
  end
end

Spec::Example::ExampleGroupMethods.module_eval do
  def track_example_run_state
    before(:each) do
      @original_rspec_options = ::Spec::Runner.options
      @options = ::Spec::Runner::Options.new(StringIO.new, @error_stream = StringIO.new)
      @example_state = ExampleState.new
      @formatter = TestingFormatter.new(@options, @error_stream, @example_state)
      class << self
        def example
          @example_state
        end
        def be_successful
          BeSuccessful.new
        end
      end
      @options.formatters << @formatter
      ::Spec::Runner.use(@options)
    end

    after(:each) do
      ::Spec::Runner.use(@original_rspec_options)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spectie-0.0.4 spec/example_run_state_tracking.rb
spectie-0.0.3 spec/example_run_state_tracking.rb