changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/spec/spec/matchers/raise_error_spec.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
3describe "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
13end
14
15describe "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
25end
26
27describe "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
44end
45
46describe "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
63end
64
65describe "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 (NameError)" do
77 lambda {
78 lambda { raise }.should raise_error(NameError)
79 }.should fail_with("expected NameError, got RuntimeError")
80 end
81
82 it "should fail if another error is raised (NameError)" do
83 lambda {
84 lambda { load "non/existent/file" }.should raise_error(NameError)
85 }.should fail_with(/expected NameError, got #<LoadError/)
86 end
87end
88
89describe "should_not raise_error(NamedError)" do
90 it "should pass if nothing is raised" do
91 lambda { }.should_not raise_error(NameError)
92 end
93
94 it "should pass if another error is raised" do
95 lambda { raise }.should_not raise_error(NameError)
96 end
97
98 it "should fail if named error is raised" do
99 lambda {
100 lambda { non_existent_method }.should_not raise_error(NameError)
101 }.should fail_with(/expected no NameError, got #<NameError: undefined/)
102 end
103end
104
105describe "should raise_error(NamedError, error_message) with String" do
106 it "should pass if named error is raised with same message" do
107 lambda { raise "example message" }.should raise_error(RuntimeError, "example message")
108 end
109
110 it "should fail if nothing is raised" do
111 lambda {
112 lambda {}.should raise_error(RuntimeError, "example message")
113 }.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
114 end
115
116 it "should fail if incorrect error is raised" do
117 lambda {
118 lambda { raise }.should raise_error(NameError, "example message")
119 }.should fail_with("expected NameError with \"example message\", got RuntimeError")
120 end
121
122 it "should fail if correct error is raised with incorrect message" do
123 lambda {
124 lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, "example message")
125 }.should fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
126 end
127end
128
129describe "should_not raise_error(NamedError, error_message) with String" do
130 it "should pass if nothing is raised" do
131 lambda {}.should_not raise_error(RuntimeError, "example message")
132 end
133
134 it "should pass if a different error is raised" do
135 lambda { raise }.should_not raise_error(NameError, "example message")
136 end
137
138 it "should pass if same error is raised with different message" do
139 lambda { raise RuntimeError.new("not the example message") }.should_not raise_error(RuntimeError, "example message")
140 end
141
142 it "should fail if named error is raised with same message" do
143 lambda {
144 lambda { raise "example message" }.should_not raise_error(RuntimeError, "example message")
145 }.should fail_with("expected no RuntimeError with \"example message\", got #<RuntimeError: example message>")
146 end
147end
148
149describe "should raise_error(NamedError, error_message) with Regexp" do
150 it "should pass if named error is raised with matching message" do
151 lambda { raise "example message" }.should raise_error(RuntimeError, /ample mess/)
152 end
153
154 it "should fail if nothing is raised" do
155 lambda {
156 lambda {}.should raise_error(RuntimeError, /ample mess/)
157 }.should fail_with("expected RuntimeError with message matching /ample mess/ but nothing was raised")
158 end
159
160 it "should fail if incorrect error is raised" do
161 lambda {
162 lambda { raise }.should raise_error(NameError, /ample mess/)
163 }.should fail_with("expected NameError with message matching /ample mess/, got RuntimeError")
164 end
165
166 it "should fail if correct error is raised with incorrect message" do
167 lambda {
168 lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, /less than ample mess/)
169 }.should fail_with("expected RuntimeError with message matching /less than ample mess/, got #<RuntimeError: not the example message>")
170 end
171end
172
173describe "should_not raise_error(NamedError, error_message) with Regexp" do
174 it "should pass if nothing is raised" do
175 lambda {}.should_not raise_error(RuntimeError, /ample mess/)
176 end
177
178 it "should pass if a different error is raised" do
179 lambda { raise }.should_not raise_error(NameError, /ample mess/)
180 end
181
182 it "should pass if same error is raised with non-matching message" do
183 lambda { raise RuntimeError.new("non matching message") }.should_not raise_error(RuntimeError, /ample mess/)
184 end
185
186 it "should fail if named error is raised with matching message" do
187 lambda {
188 lambda { raise "example message" }.should_not raise_error(RuntimeError, /ample mess/)
189 }.should fail_with("expected no RuntimeError with message matching /ample mess/, got #<RuntimeError: example message>")
190 end
191end