Commit 8979bc9713a1356891d703bfc3e26afcad3d0d80

Authored by Andrew Kane
1 parent bffa6458

Added tests for log subscriber [skip ci]

Showing 1 changed file with 41 additions and 0 deletions   Show diff stats
test/log_subscriber_test.rb 0 → 100644
... ... @@ -0,0 +1,41 @@
  1 +require_relative "test_helper"
  2 +
  3 +class LogSubscriberTest < Minitest::Test
  4 + def test_create
  5 + output = capture_logs do
  6 + Product.create!(name: "Product A")
  7 + end
  8 + assert_match "Product Store", output
  9 + end
  10 +
  11 + def test_update
  12 + product = Product.create!(name: "Product A")
  13 + output = capture_logs do
  14 + product.update!(name: "Product B")
  15 + end
  16 + assert_match "Product Store", output
  17 + end
  18 +
  19 + def test_destroy
  20 + product = Product.create!(name: "Product A")
  21 + output = capture_logs do
  22 + product.destroy
  23 + end
  24 + assert_match "Product Remove", output
  25 + end
  26 +
  27 + def capture_logs
  28 + previous_logger = ActiveSupport::LogSubscriber.logger
  29 + io = StringIO.new
  30 + begin
  31 + ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(io)
  32 + yield
  33 + io.rewind
  34 + output = io.read
  35 + previous_logger.debug(output) if previous_logger
  36 + output
  37 + ensure
  38 + ActiveSupport::LogSubscriber.logger = previous_logger
  39 + end
  40 + end
  41 +end
... ...