/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

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