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(NamedError)" do
28 it "should pass if named error is raised" do
29 lambda { non_existent_method }.should raise_error(NameError)
32 it "should fail if nothing is raised" do
34 lambda { }.should raise_error(NameError)
35 }.should fail_with("expected NameError but nothing was raised")
38 it "should fail if another error is raised" do
40 lambda { raise }.should raise_error(NameError)
41 }.should fail_with("expected NameError, got RuntimeError")
45 describe "should_not raise_error(NamedError)" do
46 it "should pass if nothing is raised" do
47 lambda { }.should_not raise_error(NameError)
50 it "should pass if another error is raised" do
51 lambda { raise }.should_not raise_error(NameError)
54 it "should fail if named error is raised" do
56 lambda { non_existent_method }.should_not raise_error(NameError)
57 }.should fail_with(/expected no NameError, got #<NameError: undefined/)
61 describe "should raise_error(NamedError, error_message) with String" do
62 it "should pass if named error is raised with same message" do
63 lambda { raise "example message" }.should raise_error(RuntimeError, "example message")
66 it "should fail if nothing is raised" do
68 lambda {}.should raise_error(RuntimeError, "example message")
69 }.should fail_with("expected RuntimeError with \"example message\" but nothing was raised")
72 it "should fail if incorrect error is raised" do
74 lambda { raise }.should raise_error(NameError, "example message")
75 }.should fail_with("expected NameError with \"example message\", got RuntimeError")
78 it "should fail if correct error is raised with incorrect message" do
80 lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, "example message")
81 }.should fail_with(/expected RuntimeError with \"example message\", got #<RuntimeError: not the example message/)
85 describe "should_not raise_error(NamedError, error_message) with String" do
86 it "should pass if nothing is raised" do
87 lambda {}.should_not raise_error(RuntimeError, "example message")
90 it "should pass if a different error is raised" do
91 lambda { raise }.should_not raise_error(NameError, "example message")
94 it "should pass if same error is raised with different message" do
95 lambda { raise RuntimeError.new("not the example message") }.should_not raise_error(RuntimeError, "example message")
98 it "should fail if named error is raised with same message" do
100 lambda { raise "example message" }.should_not raise_error(RuntimeError, "example message")
101 }.should fail_with("expected no RuntimeError with \"example message\", got #<RuntimeError: example message>")
105 describe "should raise_error(NamedError, error_message) with Regexp" do
106 it "should pass if named error is raised with matching message" do
107 lambda { raise "example message" }.should raise_error(RuntimeError, /ample mess/)
110 it "should fail if nothing is raised" do
112 lambda {}.should raise_error(RuntimeError, /ample mess/)
113 }.should fail_with("expected RuntimeError with message matching /ample mess/ but nothing was raised")
116 it "should fail if incorrect error is raised" do
118 lambda { raise }.should raise_error(NameError, /ample mess/)
119 }.should fail_with("expected NameError with message matching /ample mess/, got RuntimeError")
122 it "should fail if correct error is raised with incorrect message" do
124 lambda { raise RuntimeError.new("not the example message") }.should raise_error(RuntimeError, /less than ample mess/)
125 }.should fail_with("expected RuntimeError with message matching /less than ample mess/, got #<RuntimeError: not the example message>")
129 describe "should_not raise_error(NamedError, error_message) with Regexp" do
130 it "should pass if nothing is raised" do
131 lambda {}.should_not raise_error(RuntimeError, /ample mess/)
134 it "should pass if a different error is raised" do
135 lambda { raise }.should_not raise_error(NameError, /ample mess/)
138 it "should pass if same error is raised with non-matching message" do
139 lambda { raise RuntimeError.new("non matching message") }.should_not raise_error(RuntimeError, /ample mess/)
142 it "should fail if named error is raised with matching message" do
144 lambda { raise "example message" }.should_not raise_error(RuntimeError, /ample mess/)
145 }.should fail_with("expected no RuntimeError with message matching /ample mess/, got #<RuntimeError: example message>")