]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/spec/spec/matchers/raise_error_spec.rb
f33fba903a6952287236e6d1c4e55b08a6adf1fa
[rails.git] / vendor / gems / rspec-1.1.2 / spec / spec / matchers / raise_error_spec.rb
1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
3 describe "should raise_error" do
4   it "should pass if anything is raised" do
5     lambda {raise}.should raise_error
6   end
7   
8   it "should fail if nothing is raised" do
9     lambda {
10       lambda {}.should raise_error
11     }.should fail_with("expected Exception but nothing was raised")
12   end
13 end
14
15 describe "should_not raise_error" do
16   it "should pass if nothing is raised" do
17     lambda {}.should_not raise_error
18   end
19   
20   it "should fail if anything is raised" do
21     lambda {
22       lambda {raise}.should_not raise_error
23     }.should fail_with("expected no Exception, got RuntimeError")
24   end
25 end
26
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')
30   end
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')
33   end
34   it "should fail if RuntimeError error is raised with the wrong message" do
35     lambda do
36       lambda {raise 'blarg'}.should raise_error('blah')
37     end.should fail_with("expected Exception with \"blah\", got #<RuntimeError: blarg>")
38   end
39   it "should fail if any other error is raised with the wrong message" do
40     lambda do
41       lambda {raise NameError.new('blarg')}.should raise_error('blah')
42     end.should fail_with("expected Exception with \"blah\", got #<NameError: blarg>")
43   end
44 end
45
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')
49   end
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')
52   end
53   it "should fail if RuntimeError is raised with message" do
54     lambda do
55       lambda {raise 'blah'}.should_not raise_error('blah')
56     end.should fail_with(%Q|expected no Exception with "blah", got #<RuntimeError: blah>|)
57   end
58   it "should fail if any other error is raised with message" do
59     lambda 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>|)
62   end
63 end
64
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)
68   end
69   
70   it "should fail if nothing is raised" do
71     lambda {
72       lambda { }.should raise_error(NameError)
73     }.should fail_with("expected NameError but nothing was raised")
74   end
75   
76   it "should fail if another error is raised" do
77     lambda {
78       lambda { raise }.should raise_error(NameError)
79     }.should fail_with("expected NameError, got RuntimeError")
80   end
81 end
82
83 describe "should_not raise_error(NamedError)" do
84   it "should pass if nothing is raised" do
85     lambda { }.should_not raise_error(NameError)
86   end
87   
88   it "should pass if another error is raised" do
89     lambda { raise }.should_not raise_error(NameError)
90   end
91   
92   it "should fail if named error is raised" do
93     lambda {
94       lambda { non_existent_method }.should_not raise_error(NameError)
95     }.should fail_with(/expected no NameError, got #<NameError: undefined/)
96   end  
97 end
98
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")
102   end
103   
104   it "should fail if nothing is raised" do
105     lambda {
106       lambda {}.should raise_error(RuntimeError, "example message")
107     }.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
108   end
109   
110   it "should fail if incorrect error is raised" do
111     lambda {
112       lambda { raise }.should raise_error(NameError, "example message")
113     }.should fail_with("expected NameError with \"example message\", got RuntimeError")
114   end
115   
116   it "should fail if correct error is raised with incorrect message" do
117     lambda {
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/)
120   end
121 end
122
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")
126   end
127   
128   it "should pass if a different error is raised" do
129     lambda { raise }.should_not raise_error(NameError, "example message")
130   end
131   
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")
134   end
135   
136   it "should fail if named error is raised with same message" do
137     lambda {
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>")
140   end
141 end
142
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/)
146   end
147   
148   it "should fail if nothing is raised" do
149     lambda {
150       lambda {}.should raise_error(RuntimeError, /ample mess/)
151     }.should fail_with("expected RuntimeError with message matching /ample mess/ but nothing was raised")
152   end
153   
154   it "should fail if incorrect error is raised" do
155     lambda {
156       lambda { raise }.should raise_error(NameError, /ample mess/)
157     }.should fail_with("expected NameError with message matching /ample mess/, got RuntimeError")
158   end
159   
160   it "should fail if correct error is raised with incorrect message" do
161     lambda {
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>")
164   end
165 end
166
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/)
170   end
171   
172   it "should pass if a different error is raised" do
173     lambda { raise }.should_not raise_error(NameError, /ample mess/)
174   end
175   
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/)
178   end
179   
180   it "should fail if named error is raised with matching message" do
181     lambda {
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>")
184   end
185 end