PageRenderTime 36ms CodeModel.GetById 15ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rspec-core-2.14.8/spec/rspec/core/formatters/json_formatter_spec.rb

https://github.com/delowong/logstash
Ruby | 115 lines | 96 code | 10 blank | 9 comment | 0 complexity | 4afc5d2df0b00506e23b1fd679f4704b MD5 | raw file
  1require 'spec_helper'
  2require 'rspec/core/formatters/json_formatter'
  3require 'json'
  4require 'rspec/core/reporter'
  5
  6# todo, someday:
  7# it "lists the groups (describe and context) separately"
  8# it "includes full 'execution_result'"
  9# it "relativizes backtrace paths"
 10# it "includes profile information (implements dump_profile)"
 11# it "shows the pending message if one was given"
 12# it "shows the seed if run was randomized"
 13# it "lists pending specs that were fixed"
 14describe RSpec::Core::Formatters::JsonFormatter do
 15  let(:output) { StringIO.new }
 16  let(:formatter) { RSpec::Core::Formatters::JsonFormatter.new(output) }
 17  let(:reporter) { RSpec::Core::Reporter.new(formatter) }
 18
 19  it "outputs json (brittle high level functional test)" do
 20    group = RSpec::Core::ExampleGroup.describe("one apiece") do
 21      it("succeeds") { expect(1).to eq 1 }
 22      it("fails") { fail "eek" }
 23      it("pends") { pending "world peace" }
 24    end
 25    succeeding_line = __LINE__ - 4
 26    failing_line = __LINE__ - 4
 27    pending_line = __LINE__ - 4
 28
 29    now = Time.now
 30    Time.stub(:now).and_return(now)
 31    reporter.report(2) do |r|
 32      group.run(r)
 33    end
 34
 35    # grab the actual backtrace -- kind of a cheat
 36    failing_backtrace = formatter.output_hash[:examples][1][:exception][:backtrace]
 37    this_file = relative_path(__FILE__)
 38
 39    expected = {
 40      :examples => [
 41        {
 42          :description => "succeeds",
 43          :full_description => "one apiece succeeds",
 44          :status => "passed",
 45          :file_path => this_file,
 46          :line_number => succeeding_line,
 47        },
 48        {
 49          :description => "fails",
 50          :full_description => "one apiece fails",
 51          :status => "failed",
 52          :file_path => this_file,
 53          :line_number => failing_line,
 54          :exception => {:class => "RuntimeError", :message => "eek", :backtrace => failing_backtrace}
 55        },
 56        {
 57          :description => "pends",
 58          :full_description => "one apiece pends",
 59          :status => "pending",
 60          :file_path => this_file,
 61          :line_number => pending_line,
 62        },
 63      ],
 64      :summary => {
 65        :duration => formatter.output_hash[:summary][:duration],
 66        :example_count => 3,
 67        :failure_count => 1,
 68        :pending_count => 1,
 69      },
 70      :summary_line => "3 examples, 1 failure, 1 pending"
 71    }
 72    expect(formatter.output_hash).to eq expected
 73    expect(output.string).to eq expected.to_json
 74  end
 75
 76  describe "#stop" do
 77    it "adds all examples to the output hash" do
 78      formatter.stop
 79      expect(formatter.output_hash[:examples]).not_to be_nil
 80    end
 81  end
 82
 83  describe "#close" do
 84    it "outputs the results as a JSON string" do
 85      expect(output.string).to eq ""
 86      formatter.close
 87      expect(output.string).to eq({}.to_json)
 88    end
 89  end
 90
 91  describe "#message" do
 92    it "adds a message to the messages list" do
 93      formatter.message("good job")
 94      expect(formatter.output_hash[:messages]).to eq ["good job"]
 95    end
 96  end
 97
 98  describe "#dump_summary" do
 99    it "adds summary info to the output hash" do
100      duration, example_count, failure_count, pending_count = 1.0, 2, 1, 1
101      formatter.dump_summary(duration, example_count, failure_count, pending_count)
102      summary = formatter.output_hash[:summary]
103      %w(duration example_count failure_count pending_count).each do |key|
104        expect(summary[key.to_sym]).to eq eval(key)
105      end
106      summary_line = formatter.output_hash[:summary_line]
107      expect(summary_line).to eq "2 examples, 1 failure, 1 pending"
108    end
109
110    it "ignores --profile" do
111      allow(RSpec.configuration).to receive(:profile_examples).and_return(true)
112      formatter.dump_summary(1.0, 2, 1, 1)
113    end
114  end
115end