summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/runner/formatters/yaml_spec.rb
blob: eb4d99f74c80364677f803ae0ed6a3f79a7e3e06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require File.dirname(__FILE__) + '/../../spec_helper'
require 'mspec/runner/formatters/yaml'
require 'mspec/runner/example'

describe YamlFormatter, "#initialize" do
  it "permits zero arguments" do
    YamlFormatter.new
  end

  it "accepts one argument" do
    YamlFormatter.new nil
  end
end

describe YamlFormatter, "#print" do
  before :each do
    $stdout = IOStub.new
    @out = IOStub.new
    File.stub(:open).and_return(@out)
    @formatter = YamlFormatter.new "some/file"
  end

  after :each do
    $stdout = STDOUT
  end

  it "writes to $stdout if #switch has not been called" do
    @formatter.print "begonias"
    $stdout.should == "begonias"
    @out.should == ""
  end

  it "writes to the file passed to #initialize once #switch has been called" do
    @formatter.switch
    @formatter.print "begonias"
    $stdout.should == ""
    @out.should == "begonias"
  end

  it "writes to $stdout once #switch is called if no file was passed to #initialize" do
    formatter = YamlFormatter.new
    formatter.switch
    formatter.print "begonias"
    $stdout.should == "begonias"
    @out.should == ""
  end
end

describe YamlFormatter, "#finish" do
  before :each do
    @tally = double("tally").as_null_object
    @counter = double("counter").as_null_object
    @tally.stub(:counter).and_return(@counter)
    TallyAction.stub(:new).and_return(@tally)

    @timer = double("timer").as_null_object
    TimerAction.stub(:new).and_return(@timer)

    $stdout = IOStub.new
    context = ContextState.new "describe"
    @state = ExampleState.new(context, "it")

    @formatter = YamlFormatter.new
    @formatter.stub(:backtrace).and_return("")
    MSpec.stub(:register)
    @formatter.register

    exc = ExceptionState.new @state, nil, MSpecExampleError.new("broken")
    exc.stub(:backtrace).and_return("path/to/some/file.rb:35:in method")
    @formatter.exception exc
    @formatter.after @state
  end

  after :each do
    $stdout = STDOUT
  end

  it "calls #switch" do
    @formatter.should_receive(:switch)
    @formatter.finish
  end

  it "outputs a failure message and backtrace" do
    @formatter.finish
    $stdout.should include "describe it ERROR"
    $stdout.should include "MSpecExampleError: broken\\n"
    $stdout.should include "path/to/some/file.rb:35:in method"
  end

  it "outputs an elapsed time" do
    @timer.should_receive(:elapsed).and_return(4.2)
    @formatter.finish
    $stdout.should include "time: 4.2"
  end

  it "outputs a file count" do
    @counter.should_receive(:files).and_return(3)
    @formatter.finish
    $stdout.should include "files: 3"
  end

  it "outputs an example count" do
    @counter.should_receive(:examples).and_return(3)
    @formatter.finish
    $stdout.should include "examples: 3"
  end

  it "outputs an expectation count" do
    @counter.should_receive(:expectations).and_return(9)
    @formatter.finish
    $stdout.should include "expectations: 9"
  end

  it "outputs a failure count" do
    @counter.should_receive(:failures).and_return(2)
    @formatter.finish
    $stdout.should include "failures: 2"
  end

  it "outputs an error count" do
    @counter.should_receive(:errors).and_return(1)
    @formatter.finish
    $stdout.should include "errors: 1"
  end
end