/test/notifier_test.rb

https://github.com/indirect/airbrake · Ruby · 235 lines · 181 code · 54 blank · 0 comment · 5 complexity · a0942816e337335712baaaf82885d2e4 MD5 · raw file

  1. require File.dirname(__FILE__) + '/helper'
  2. class NotifierTest < Test::Unit::TestCase
  3. class OriginalException < Exception
  4. end
  5. class ContinuedException < Exception
  6. end
  7. include DefinesConstants
  8. def setup
  9. super
  10. reset_config
  11. end
  12. def assert_sent(notice, notice_args)
  13. assert_received(Airbrake::Notice, :new) {|expect| expect.with(has_entries(notice_args)) }
  14. assert_received(notice, :to_xml)
  15. assert_received(Airbrake.sender, :send_to_airbrake) {|expect| expect.with(notice.to_xml) }
  16. end
  17. def set_public_env
  18. Airbrake.configure { |config| config.environment_name = 'production' }
  19. end
  20. def set_development_env
  21. Airbrake.configure { |config| config.environment_name = 'development' }
  22. end
  23. should "yield and save a configuration when configuring" do
  24. yielded_configuration = nil
  25. Airbrake.configure do |config|
  26. yielded_configuration = config
  27. end
  28. assert_kind_of Airbrake::Configuration, yielded_configuration
  29. assert_equal yielded_configuration, Airbrake.configuration
  30. end
  31. should "not remove existing config options when configuring twice" do
  32. first_config = nil
  33. Airbrake.configure do |config|
  34. first_config = config
  35. end
  36. Airbrake.configure do |config|
  37. assert_equal first_config, config
  38. end
  39. end
  40. should "configure the sender" do
  41. sender = stub_sender
  42. Airbrake::Sender.stubs(:new => sender)
  43. configuration = nil
  44. Airbrake.configure { |yielded_config| configuration = yielded_config }
  45. assert_received(Airbrake::Sender, :new) { |expect| expect.with(configuration) }
  46. assert_equal sender, Airbrake.sender
  47. end
  48. should "create and send a notice for an exception" do
  49. set_public_env
  50. exception = build_exception
  51. stub_sender!
  52. notice = stub_notice!
  53. Airbrake.notify(exception)
  54. assert_sent notice, :exception => exception
  55. end
  56. should "create and send a notice for a hash" do
  57. set_public_env
  58. notice = stub_notice!
  59. notice_args = { :error_message => 'uh oh' }
  60. stub_sender!
  61. Airbrake.notify(notice_args)
  62. assert_sent(notice, notice_args)
  63. end
  64. should "create and send a notice for an exception that responds to to_hash" do
  65. set_public_env
  66. exception = build_exception
  67. notice = stub_notice!
  68. notice_args = { :error_message => 'uh oh' }
  69. exception.stubs(:to_hash).returns(notice_args)
  70. stub_sender!
  71. Airbrake.notify(exception)
  72. assert_sent(notice, notice_args.merge(:exception => exception))
  73. end
  74. should "create and sent a notice for an exception and hash" do
  75. set_public_env
  76. exception = build_exception
  77. notice = stub_notice!
  78. notice_args = { :error_message => 'uh oh' }
  79. stub_sender!
  80. Airbrake.notify(exception, notice_args)
  81. assert_sent(notice, notice_args.merge(:exception => exception))
  82. end
  83. should "not create a notice in a development environment" do
  84. set_development_env
  85. sender = stub_sender!
  86. Airbrake.notify(build_exception)
  87. Airbrake.notify_or_ignore(build_exception)
  88. assert_received(sender, :send_to_airbrake) {|expect| expect.never }
  89. end
  90. should "not deliver an ignored exception when notifying implicitly" do
  91. set_public_env
  92. exception = build_exception
  93. sender = stub_sender!
  94. notice = stub_notice!
  95. notice.stubs(:ignore? => true)
  96. Airbrake.notify_or_ignore(exception)
  97. assert_received(sender, :send_to_airbrake) {|expect| expect.never }
  98. end
  99. should "deliver an ignored exception when notifying manually" do
  100. set_public_env
  101. exception = build_exception
  102. sender = stub_sender!
  103. notice = stub_notice!
  104. notice.stubs(:ignore? => true)
  105. Airbrake.notify(exception)
  106. assert_sent(notice, :exception => exception)
  107. end
  108. should "pass config to created notices" do
  109. exception = build_exception
  110. config_opts = { 'one' => 'two', 'three' => 'four' }
  111. notice = stub_notice!
  112. stub_sender!
  113. Airbrake.configuration = stub('config', :merge => config_opts, :public? => true)
  114. Airbrake.notify(exception)
  115. assert_received(Airbrake::Notice, :new) do |expect|
  116. expect.with(has_entries(config_opts))
  117. end
  118. end
  119. context "building notice JSON for an exception" do
  120. setup do
  121. @params = { :controller => "users", :action => "create" }
  122. @exception = build_exception
  123. @hash = Airbrake.build_lookup_hash_for(@exception, @params)
  124. end
  125. should "set action" do
  126. assert_equal @params[:action], @hash[:action]
  127. end
  128. should "set controller" do
  129. assert_equal @params[:controller], @hash[:component]
  130. end
  131. should "set line number" do
  132. assert @hash[:line_number] =~ /\d+/
  133. end
  134. should "set file" do
  135. assert_match /test\/helper\.rb$/, @hash[:file]
  136. end
  137. should "set rails_env to production" do
  138. assert_equal 'production', @hash[:environment_name]
  139. end
  140. should "set error class" do
  141. assert_equal @exception.class.to_s, @hash[:error_class]
  142. end
  143. should "not set file or line number with no backtrace" do
  144. @exception.stubs(:backtrace).returns([])
  145. @hash = Airbrake.build_lookup_hash_for(@exception)
  146. assert_nil @hash[:line_number]
  147. assert_nil @hash[:file]
  148. end
  149. should "not set action or controller when not provided" do
  150. @hash = Airbrake.build_lookup_hash_for(@exception)
  151. assert_nil @hash[:action]
  152. assert_nil @hash[:controller]
  153. end
  154. context "when an exception that provides #original_exception is raised" do
  155. setup do
  156. @exception.stubs(:original_exception).returns(begin
  157. raise NotifierTest::OriginalException.new
  158. rescue Exception => e
  159. e
  160. end)
  161. end
  162. should "unwrap exceptions that provide #original_exception" do
  163. @hash = Airbrake.build_lookup_hash_for(@exception)
  164. assert_equal "NotifierTest::OriginalException", @hash[:error_class]
  165. end
  166. end
  167. context "when an exception that provides #continued_exception is raised" do
  168. setup do
  169. @exception.stubs(:continued_exception).returns(begin
  170. raise NotifierTest::ContinuedException.new
  171. rescue Exception => e
  172. e
  173. end)
  174. end
  175. should "unwrap exceptions that provide #continued_exception" do
  176. @hash = Airbrake.build_lookup_hash_for(@exception)
  177. assert_equal "NotifierTest::ContinuedException", @hash[:error_class]
  178. end
  179. end
  180. end
  181. end