log_subscriber_test.rb
977 Bytes
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
require_relative "test_helper"
class LogSubscriberTest < Minitest::Test
def test_create
output = capture_logs do
Product.create!(name: "Product A")
end
assert_match "Product Store", output
end
def test_update
product = Product.create!(name: "Product A")
output = capture_logs do
product.update!(name: "Product B")
end
assert_match "Product Store", output
end
def test_destroy
product = Product.create!(name: "Product A")
output = capture_logs do
product.destroy
end
assert_match "Product Remove", output
end
def capture_logs
previous_logger = ActiveSupport::LogSubscriber.logger
io = StringIO.new
begin
ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(io)
yield
io.rewind
output = io.read
previous_logger.debug(output) if previous_logger
output
ensure
ActiveSupport::LogSubscriber.logger = previous_logger
end
end
end