summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/runner/exception_spec.rb
blob: 0e0a819992a35dd98b95cde99f97a2d0fe40c461 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'spec_helper'
require 'mspec/expectations/expectations'
require 'mspec/runner/example'
require 'mspec/runner/exception'
require 'mspec/utils/script'

describe ExceptionState, "#initialize" do
  it "takes a state, location (e.g. before :each), and exception" do
    context = ContextState.new "Class#method"
    state = ExampleState.new context, "does something"
    exc = Exception.new "Fail!"
    ExceptionState.new(state, "location", exc).should be_kind_of(ExceptionState)
  end
end

describe ExceptionState, "#description" do
  before :each do
    context = ContextState.new "Class#method"
    @state = ExampleState.new context, "does something"
  end

  it "returns the state description if state was not nil" do
    exc = ExceptionState.new(@state, nil, nil)
    exc.description.should == "Class#method does something"
  end

  it "returns the location if it is not nil and description is nil" do
    exc = ExceptionState.new(nil, "location", nil)
    exc.description.should == "An exception occurred during: location"
  end

  it "returns both description and location if neither are nil" do
    exc = ExceptionState.new(@state, "location", nil)
    exc.description.should == "An exception occurred during: location\nClass#method does something"
  end
end

describe ExceptionState, "#describe" do
  before :each do
    context = ContextState.new "Class#method"
    @state = ExampleState.new context, "does something"
  end

  it "returns the ExampleState#describe string if created with a non-nil state" do
    ExceptionState.new(@state, nil, nil).describe.should == @state.describe
  end

  it "returns an empty string if created with a nil state" do
    ExceptionState.new(nil, nil, nil).describe.should == ""
  end
end

describe ExceptionState, "#it" do
  before :each do
    context = ContextState.new "Class#method"
    @state = ExampleState.new context, "does something"
  end

  it "returns the ExampleState#it string if created with a non-nil state" do
    ExceptionState.new(@state, nil, nil).it.should == @state.it
  end

  it "returns an empty string if created with a nil state" do
    ExceptionState.new(nil, nil, nil).it.should == ""
  end
end

describe ExceptionState, "#failure?" do
  before :each do
    @state = ExampleState.new ContextState.new("C#m"), "works"
  end

  it "returns true if the exception is an SpecExpectationNotMetError" do
    exc = ExceptionState.new @state, "", SpecExpectationNotMetError.new("Fail!")
    exc.failure?.should be_true
  end

  it "returns true if the exception is an SpecExpectationNotFoundError" do
    exc = ExceptionState.new @state, "", SpecExpectationNotFoundError.new("Fail!")
    exc.failure?.should be_true
  end

  it "returns false if the exception is not an SpecExpectationNotMetError or an SpecExpectationNotFoundError" do
    exc = ExceptionState.new @state, "", Exception.new("Fail!")
    exc.failure?.should be_false
  end
end

describe ExceptionState, "#message" do
  before :each do
    @state = ExampleState.new ContextState.new("C#m"), "works"
  end

  it "returns <No message> if the exception message is empty" do
    exc = ExceptionState.new @state, "", Exception.new("")
    exc.message.should == "Exception: <No message>"
  end

  it "returns the message without exception class when the exception is an SpecExpectationNotMetError" do
    exc = ExceptionState.new @state, "", SpecExpectationNotMetError.new("Fail!")
    exc.message.should == "Fail!"
  end

  it "returns SpecExpectationNotFoundError#message when the exception is an SpecExpectationNotFoundError" do
    e = SpecExpectationNotFoundError.new
    exc = ExceptionState.new @state, "", e
    exc.message.should == e.message
  end

  it "returns the message with exception class when the exception is not an SpecExpectationNotMetError or an SpecExpectationNotFoundError" do
    exc = ExceptionState.new @state, "", Exception.new("Fail!")
    exc.message.should == "Exception: Fail!"
  end
end

describe ExceptionState, "#backtrace" do
  before :each do
    @state = ExampleState.new ContextState.new("C#m"), "works"
    begin
      raise Exception
    rescue Exception => @exception
      @exc = ExceptionState.new @state, "", @exception
    end
  end

  after :each do
    $MSPEC_DEBUG = nil
  end

  it "returns a string representation of the exception backtrace" do
    @exc.backtrace.should be_kind_of(String)
  end

  it "does not filter files from the backtrace if $MSPEC_DEBUG is true" do
    $MSPEC_DEBUG = true
    @exc.backtrace.should == @exception.backtrace.join("\n")
  end

  it "filters files matching config[:backtrace_filter]" do
    MSpecScript.set :backtrace_filter, %r[mspec/lib]
    $MSPEC_DEBUG = nil
    @exc.backtrace.split("\n").each do |line|
      line.should_not =~ %r[mspec/lib]
    end
  end
end