/
not_rescue_exception_spec.rb
executable file
·95 lines (88 loc) · 2.87 KB
/
not_rescue_exception_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'spec_helper'
module RailsBestPractices
module Reviews
describe NotRescueExceptionReview do
let(:runner) { Core::Runner.new(reviews: NotRescueExceptionReview.new) }
describe "not_rescue_exception" do
it "should not rescue exception in method rescue with named var" do
content =<<-EOF
def my_method
do_something
rescue Exception => e
logger.error e
end
EOF
runner.review('app/helpers/posts_helper.rb', content)
runner.should have(1).errors
runner.errors[0].to_s.should == "app/helpers/posts_helper.rb:3 - not rescue Exception"
end
it "should not rescue exception in method rescue without named var" do
content =<<-EOF
def my_method
do_something
rescue Exception
logger.error $!
end
EOF
runner.review('app/helpers/posts_helper.rb', content)
runner.should have(1).errors
runner.errors[0].to_s.should == "app/helpers/posts_helper.rb:3 - not rescue Exception"
end
it "should not rescue exception in block rescue with named var" do
content =<<-EOF
def my_method
begin
do_something
rescue Exception => e
logger.error e
end
end
EOF
runner.review('app/helpers/posts_helper.rb', content)
runner.should have(1).errors
runner.errors[0].to_s.should == "app/helpers/posts_helper.rb:4 - not rescue Exception"
end
it "should not rescue exception in block rescue without named var" do
content =<<-EOF
def my_method
begin
do_something
rescue Exception
logger.error $!
end
end
EOF
runner.review('app/helpers/posts_helper.rb', content)
runner.should have(1).errors
runner.errors[0].to_s.should == "app/helpers/posts_helper.rb:4 - not rescue Exception"
end
it "should allow rescue implicit StandardError in block rescue without named var" do
content =<<-EOF
def my_method
begin
do_something
rescue
logger.error $!
end
end
EOF
runner.review('app/helpers/posts_helper.rb', content)
runner.should have(0).errors
end
it "should allow rescue explicit StandardError in block rescue without named var" do
content =<<-EOF
def my_method
begin
do_something
rescue StandardError
logger.error $!
end
end
EOF
runner.review('app/helpers/posts_helper.rb', content)
runner.should have(0).errors
end
end
end
end
end