forked from rails/exception_notification
-
Notifications
You must be signed in to change notification settings - Fork 414
/
formatter_test.rb
140 lines (116 loc) · 3.74 KB
/
formatter_test.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'test_helper'
require 'timecop'
class FormatterTest < ActiveSupport::TestCase
class HomeController < ActionController::Metal
def index; end
end
setup do
@exception = RuntimeError.new('test')
Timecop.freeze('2018-12-09 12:07:16 UTC')
end
teardown do
Timecop.return
end
#
# #title
#
test 'title returns correct content' do
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_equal '⚠️ Error occurred in test ⚠️', formatter.title
end
#
# #subtitle
#
test 'subtitle without accumulated error' do
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_equal 'A *RuntimeError* occurred.', formatter.subtitle
end
test 'subtitle with accumulated error' do
formatter = ExceptionNotifier::Formatter.new(@exception, accumulated_errors_count: 3)
assert_equal '3 *RuntimeError* occurred.', formatter.subtitle
end
test 'subtitle with controller' do
controller = HomeController.new
controller.process(:index)
env = Rack::MockRequest.env_for(
'/', 'action_controller.instance' => controller
)
formatter = ExceptionNotifier::Formatter.new(@exception, env: env)
assert_equal 'A *RuntimeError* occurred in *home#index*.', formatter.subtitle
end
#
# #app_name
#
test 'app_name defaults to Rails app name' do
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_equal 'dummy', formatter.app_name
end
test 'app_name can be overwritten using options' do
formatter = ExceptionNotifier::Formatter.new(@exception, app_name: 'test')
assert_equal 'test', formatter.app_name
end
#
# #request_message
#
test 'request_message when env set' do
text = [
'```',
'* url : http://test.address/?id=foo',
'* http_method : GET',
'* ip_address : 127.0.0.1',
'* parameters : {"id"=>"foo"}',
'* timestamp : 2018-12-09 12:07:16 UTC',
'```'
].join("\n")
env = Rack::MockRequest.env_for(
'/',
'HTTP_HOST' => 'test.address',
'REMOTE_ADDR' => '127.0.0.1',
params: { id: 'foo' }
)
formatter = ExceptionNotifier::Formatter.new(@exception, env: env)
assert_equal text, formatter.request_message
end
test 'request_message when env not set' do
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_nil formatter.request_message
end
#
# #backtrace_message
#
test 'backtrace_message when backtrace set' do
text = [
'```',
"* app/controllers/my_controller.rb:53:in `my_controller_params'",
"* app/controllers/my_controller.rb:34:in `update'",
'```'
].join("\n")
@exception.set_backtrace([
"app/controllers/my_controller.rb:53:in `my_controller_params'",
"app/controllers/my_controller.rb:34:in `update'"
])
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_equal text, formatter.backtrace_message
end
test 'backtrace_message when no backtrace' do
formatter = ExceptionNotifier::Formatter.new(@exception)
assert_nil formatter.backtrace_message
end
#
# #controller_and_action
#
test 'correct controller_and_action if controller is present' do
controller = HomeController.new
controller.process(:index)
env = Rack::MockRequest.env_for(
'/', 'action_controller.instance' => controller
)
formatter = ExceptionNotifier::Formatter.new(@exception, env: env)
assert_equal 'home#index', formatter.controller_and_action
end
test 'controller_and_action is nil if no controller' do
env = Rack::MockRequest.env_for('/')
formatter = ExceptionNotifier::Formatter.new(@exception, env: env)
assert_nil formatter.controller_and_action
end
end