1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
3 describe "should raise_error" do
4 it "should pass if anything is raised" do
5 lambda {raise}.should raise_error
8 it "should fail if nothing is raised" do
10 lambda {}.should raise_error
11 }.should fail_with("expected Exception but nothing was raised")
15 describe "should_not raise_error" do
16 it "should pass if nothing is raised" do
17 lambda {}.should_not raise_error
20 it "should fail if anything is raised" do
22 lambda {raise}.should_not raise_error
23 }.should fail_with("expected no Exception, got RuntimeError")
27 describe "should raise_error(message)" do
28 it "should pass if RuntimeError is raised with the right message" do
29 lambda {raise 'blah'}.should raise_error('blah')
31 it "should pass if any other error is raised with the right message" do
32 lambda {raise NameError.new('blah')}.should raise_error('blah')
34 it "should fail if RuntimeError error is raised with the wrong message" do
36 lambda {raise 'blarg'}.should raise_error('blah')
37 end.should fail_with("expected Exception with \"blah\", got #<RuntimeError: blarg>")
39 it "should fail if any other error is raised with the wrong message" do
41 lambda {raise NameError.new('blarg')}.should raise_error('blah')
42 end.should fail_with("expected Exception with \"blah\", got #<NameError: blarg>")
46 describe "should_not raise_error(message)" do
47 it "should pass if RuntimeError error is raised with the different message" do
48 lambda {raise 'blarg'}.should_not raise_error('blah')
50 it "should pass if any other error is raised with the wrong message" do
51 lambda {raise NameError.new('blarg')}.should_not raise_error('blah')
53 it "should fail if RuntimeError is raised with message" do
55 lambda {raise 'blah'}.should_not raise_error('blah')
56 end.should fail_with(%Q|expected no Exception with "blah", got #<RuntimeError: blah>|)
58 it "should fail if any other error is raised with message" do
60 lambda {raise NameError.new('blah')}.should_not raise_error('blah')
61 end.should fail_with(%Q|expected no Exception with "blah", got #<NameError: blah>|)
65 describe "should raise_error(NamedError)" do
66 it "should pass if named error is raised" do
67 lambda { non_existent_method }.should raise_error(NameError)
70 it "should fail if nothing is raised" do
72 lambda { }.should raise_error(NameError)
73 }.should fail_with("expected NameError but nothing was raised")
76 it "should fail if another error is raised" do
78 lambda { raise }.should raise_error(NameError)
79 }.should fail_with("expected NameError, got RuntimeError")
83 describe "should_not raise_error(NamedError)" do
84 it "should pass if nothing is raised" do
85 lambda { }.should_not raise_error(NameError)
88 it "should pass if another error is raised" do
89 lambda { raise }.should_not raise_error(NameError)
92 it "should fail if named error is raised" do
94 lambda { non_existent_method }.should_not raise_error(NameError)
95 }.should fail_with(/expected no NameError, got #<NameError: undefined/)
99 describe "should raise_error(NamedError, error_message) with String" do
100 it "should pass if named error is raised with same message" do
101 lambda { raise "example message" }.should raise_error(RuntimeError, "example message")
104 it "should fail if nothing is raised" do
106 lambda {}.should raise_error(RuntimeError, "example message")
107 }.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
110 it "should fail if incorrect error is raised" do
112 lambda { raise }.should raise_error(NameError, "example message")
113 }.should fail_with("expected NameError with \"example message\", got RuntimeError")
116 it "should fail if correct error is raised with incorrect message" do
118 lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, "example message")
119 }.should fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
123 describe "should_not raise_error(NamedError, error_message) with String" do
124 it "should pass if nothing is raised" do
125 lambda {}.should_not raise_error(RuntimeError, "example message")
128 it "should pass if a different error is raised" do
129 lambda { raise }.should_not raise_error(NameError, "example message")
132 it "should pass if same error is raised with different message" do
133 lambda { raise RuntimeError.new("not the example message") }.should_not raise_error(RuntimeError, "example message")
136 it "should fail if named error is raised with same message" do
138 lambda { raise "example message" }.should_not raise_error(RuntimeError, "example message")
139 }.should fail_with("expected no RuntimeError with \"example message\", got #<RuntimeError: example message>")
143 describe "should raise_error(NamedError, error_message) with Regexp" do
144 it "should pass if named error is raised with matching message" do
145 lambda { raise "example message" }.should raise_error(RuntimeError, /ample mess/)
148 it "should fail if nothing is raised" do
150 lambda {}.should raise_error(RuntimeError, /ample mess/)
151 }.should fail_with("expected RuntimeError with message matching /ample mess/ but nothing was raised")
154 it "should fail if incorrect error is raised" do
156 lambda { raise }.should raise_error(NameError, /ample mess/)
157 }.should fail_with("expected NameError with message matching /ample mess/, got RuntimeError")
160 it "should fail if correct error is raised with incorrect message" do
162 lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, /less than ample mess/)
163 }.should fail_with("expected RuntimeError with message matching /less than ample mess/, got #<RuntimeError: not the example message>")
167 describe "should_not raise_error(NamedError, error_message) with Regexp" do
168 it "should pass if nothing is raised" do
169 lambda {}.should_not raise_error(RuntimeError, /ample mess/)
172 it "should pass if a different error is raised" do
173 lambda { raise }.should_not raise_error(NameError, /ample mess/)
176 it "should pass if same error is raised with non-matching message" do
177 lambda { raise RuntimeError.new("non matching message") }.should_not raise_error(RuntimeError, /ample mess/)
180 it "should fail if named error is raised with matching message" do
182 lambda { raise "example message" }.should_not raise_error(RuntimeError, /ample mess/)
183 }.should fail_with("expected no RuntimeError with message matching /ample mess/, got #<RuntimeError: example message>")