1 require File.dirname(__FILE__) + '/../spec_helper'
2 require 'mspec/expectations/expectations'
3 require 'mspec/runner/example'
4 require 'mspec/runner/exception'
6 describe ExceptionState, "#initialize" do
7 it "takes a state, location (e.g. before :each), and exception" do
8 context = ContextState.new "Class#method"
9 state = ExampleState.new context, "does something"
10 exc = Exception.new "Fail!"
11 ExceptionState.new(state, "location", exc).should be_kind_of(ExceptionState)
15 describe ExceptionState, "#description" do
17 context = ContextState.new "Class#method"
18 @state = ExampleState.new context, "does something"
21 it "returns the state description if state was not nil" do
22 exc = ExceptionState.new(@state, nil, nil)
23 exc.description.should == "Class#method does something"
26 it "returns the location if it is not nil and description is nil" do
27 exc = ExceptionState.new(nil, "location", nil)
28 exc.description.should == "An exception occurred during: location"
31 it "returns both description and location if neither are nil" do
32 exc = ExceptionState.new(@state, "location", nil)
33 exc.description.should == "An exception occurred during: location\nClass#method does something"
37 describe ExceptionState, "#describe" do
39 context = ContextState.new "Class#method"
40 @state = ExampleState.new context, "does something"
43 it "returns the ExampleState#describe string if created with a non-nil state" do
44 ExceptionState.new(@state, nil, nil).describe.should == @state.describe
47 it "returns an empty string if created with a nil state" do
48 ExceptionState.new(nil, nil, nil).describe.should == ""
52 describe ExceptionState, "#it" do
54 context = ContextState.new "Class#method"
55 @state = ExampleState.new context, "does something"
58 it "returns the ExampleState#it string if created with a non-nil state" do
59 ExceptionState.new(@state, nil, nil).it.should == @state.it
62 it "returns an empty string if created with a nil state" do
63 ExceptionState.new(nil, nil, nil).it.should == ""
67 describe ExceptionState, "#failure?" do
69 @state = ExampleState.new ContextState.new("C#m"), "works"
72 it "returns true if the exception is an ExpectationNotMetError" do
73 exc = ExceptionState.new @state, "", ExpectationNotMetError.new("Fail!")
74 exc.failure?.should be_true
77 it "returns true if the exception is an ExpectationNotFoundError" do
78 exc = ExceptionState.new @state, "", ExpectationNotFoundError.new("Fail!")
79 exc.failure?.should be_true
82 it "returns false if the exception is not an ExpectationNotMetError or an ExpectationNotFoundError" do
83 exc = ExceptionState.new @state, "", Exception.new("Fail!")
84 exc.failure?.should be_false
88 describe ExceptionState, "#message" do
89 it "returns <No message> if the exception message is empty" do
90 exc = ExceptionState.new @state, "", Exception.new("")
91 exc.message.should == "<No message>"
94 it "returns the message without exception class when the exception is an ExpectationNotMetError" do
95 exc = ExceptionState.new @state, "", ExpectationNotMetError.new("Fail!")
96 exc.message.should == "Fail!"
99 it "returns ExpectationNotFoundError#message when the exception is an ExpectationNotFoundError" do
100 e = ExpectationNotFoundError.new
101 exc = ExceptionState.new @state, "", e
102 exc.message.should == e.message
105 it "returns the message with exception class when the exception is not an ExpectationNotMetError or an ExpectationNotFoundError" do
106 exc = ExceptionState.new @state, "", Exception.new("Fail!")
107 exc.message.should == "Exception: Fail!"
111 describe ExceptionState, "#backtrace" do
113 @action = mock("action")
114 def @action.exception(exc)
115 ScratchPad.record exc.exception
117 MSpec.register :exception, @action
120 MSpec.protect("") { raise Exception }
122 @exc = ExceptionState.new @state, "", ScratchPad.recorded
125 it "returns a string representation of the exception backtrace" do
126 @exc.backtrace.should be_kind_of(String)
129 it "strips MSpec files from the backtrace" do
130 @exc.backtrace.split("\n").each do |line|
131 line.should_not =~ ExceptionState::PATH