rails--rails/railties/test/application/notifications_test.rb

76 lines
1.7 KiB
Ruby
Raw Normal View History

2009-10-09 12:17:08 +00:00
require "isolation/abstract_unit"
module ApplicationTests
class MyQueue
def publish(name, *args)
raise name
end
2009-10-09 12:17:08 +00:00
# Not a full queue implementation
def method_missing(name, *args, &blk)
self
2009-10-09 12:17:08 +00:00
end
end
2010-01-12 16:48:09 +00:00
class MockLogger
def method_missing(*args)
@logged ||= []
@logged << args.last
end
def logged
@logged.compact.map { |l| l.to_s.strip }
end
end
class NotificationsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
2009-10-09 12:17:08 +00:00
def setup
build_app
boot_rails
2010-01-12 16:48:09 +00:00
end
def instrument(*args, &block)
ActiveSupport::Notifications.instrument(*args, &block)
end
def wait
ActiveSupport::Notifications.notifier.wait
end
test "new queue is set" do
# We don't want to load all frameworks, so remove them and clean up environments.
use_frameworks []
FileUtils.rm_rf("#{app_path}/config/environments")
add_to_config <<-RUBY
config.notifications.notifier = ActiveSupport::Notifications::Notifier.new(ApplicationTests::MyQueue.new)
RUBY
2009-10-09 12:17:08 +00:00
require "#{app_path}/config/environment"
assert_raise RuntimeError do
ActiveSupport::Notifications.publish('foo')
end
2009-10-09 12:17:08 +00:00
end
2010-01-12 16:48:09 +00:00
test "rails subscribers are added" do
add_to_config <<-RUBY
config.colorize_logging = false
RUBY
require "#{app_path}/config/environment"
ActiveRecord::Base.logger = logger = MockLogger.new
# Mimic ActiveRecord notifications
2010-01-12 16:48:09 +00:00
instrument "active_record.sql", :name => "SQL", :sql => "SHOW tables"
wait
assert_equal 1, logger.logged.size
assert_match /SHOW tables/, logger.logged.last
end
2009-10-09 12:17:08 +00:00
end
end