diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-17 03:20:30 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-17 03:20:30 +0530 |
commit | b04230e3bbf912d60601e9e7b797c4cd43581d51 (patch) | |
tree | 97a2f784a2ec2bfae4f960af56a9280dad6f7774 /railties/test/subscriber_test.rb | |
parent | 867829b187969607aa12f2b0457f25da9c204db0 (diff) | |
parent | 6e3bee6cf1f0d2684152292db0a8b757249824fd (diff) | |
download | rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.tar.gz rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.tar.bz2 rails-b04230e3bbf912d60601e9e7b797c4cd43581d51.zip |
Merge remote branch 'mainstream/master'
Conflicts:
actionpack/lib/action_controller/metal/flash.rb
Diffstat (limited to 'railties/test/subscriber_test.rb')
-rw-r--r-- | railties/test/subscriber_test.rb | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/railties/test/subscriber_test.rb b/railties/test/subscriber_test.rb new file mode 100644 index 0000000000..ac34939510 --- /dev/null +++ b/railties/test/subscriber_test.rb @@ -0,0 +1,130 @@ +require 'abstract_unit' +require 'rails/subscriber/test_helper' + +class MySubscriber < Rails::Subscriber + attr_reader :event + + def some_event(event) + @event = event + info event.name + end + + def foo(event) + debug "debug" + info "info" + warn "warn" + end + + def bar(event) + info "#{color("cool", :red)}, #{color("isn't it?", :blue, true)}" + end +end + +module SubscriberTest + def setup + super + @subscriber = MySubscriber.new + Rails::Subscriber.instance_variable_set(:@log_tailer, nil) + end + + def teardown + super + Rails::Subscriber.subscribers.clear + Rails::Subscriber.instance_variable_set(:@log_tailer, nil) + end + + def instrument(*args, &block) + ActiveSupport::Notifications.instrument(*args, &block) + end + + def test_proxies_method_to_rails_logger + @subscriber.foo(nil) + assert_equal %w(debug), @logger.logged(:debug) + assert_equal %w(info), @logger.logged(:info) + assert_equal %w(warn), @logger.logged(:warn) + end + + def test_set_color_for_messages + Rails::Subscriber.colorize_logging = true + @subscriber.bar(nil) + assert_equal "\e[31mcool\e[0m, \e[1m\e[34misn't it?\e[0m", @logger.logged(:info).last + end + + def test_does_not_set_color_if_colorize_logging_is_set_to_false + @subscriber.bar(nil) + assert_equal "cool, isn't it?", @logger.logged(:info).last + end + + def test_event_is_sent_to_the_registered_class + Rails::Subscriber.add :my_subscriber, @subscriber + instrument "my_subscriber.some_event" + wait + assert_equal %w(my_subscriber.some_event), @logger.logged(:info) + end + + def test_event_is_an_active_support_notifications_event + Rails::Subscriber.add :my_subscriber, @subscriber + instrument "my_subscriber.some_event" + wait + assert_kind_of ActiveSupport::Notifications::Event, @subscriber.event + end + + def test_does_not_send_the_event_if_it_doesnt_match_the_class + Rails::Subscriber.add :my_subscriber, @subscriber + instrument "my_subscriber.unknown_event" + wait + # If we get here, it means that NoMethodError was raised. + end + + def test_does_not_send_the_event_if_logger_is_nil + Rails.logger = nil + @subscriber.expects(:some_event).never + Rails::Subscriber.add :my_subscriber, @subscriber + instrument "my_subscriber.some_event" + wait + end + + def test_flushes_loggers + Rails::Subscriber.add :my_subscriber, @subscriber + Rails::Subscriber.flush_all! + assert_equal 1, @logger.flush_count + end + + def test_flushes_loggers_when_action_dispatch_callback_is_received + Rails::Subscriber.add :my_subscriber, @subscriber + instrument "action_dispatch.callback" + wait + assert_equal 1, @logger.flush_count + end + + def test_flushes_the_same_logger_just_once + Rails::Subscriber.add :my_subscriber, @subscriber + Rails::Subscriber.add :another, @subscriber + instrument "action_dispatch.callback" + wait + assert_equal 1, @logger.flush_count + end + + def test_tails_logs_when_action_dispatch_callback_is_received + log_tailer = mock() + log_tailer.expects(:tail!) + Rails::Subscriber.log_tailer = log_tailer + + Rails::Subscriber.add :my_subscriber, @subscriber + instrument "action_dispatch.callback" + wait + ensure + Rails::Subscriber.log_tailer = nil + end + + class SyncSubscriberTest < ActiveSupport::TestCase + include Rails::Subscriber::SyncTestHelper + include SubscriberTest + end + + class AsyncSubscriberTest < ActiveSupport::TestCase + include Rails::Subscriber::AsyncTestHelper + include SubscriberTest + end + +end
\ No newline at end of file |