File: structured_spec.rb

package info (click to toggle)
ruby-mail-room 0.10.0%2Breally0.0.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 376 kB
  • sloc: ruby: 1,749; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,355 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'spec_helper'

describe MailRoom::Logger::Structured do

  subject { described_class.new $stdout }

  let!(:now) { Time.now }
  let(:message) { { action: 'exciting development', message: 'testing 123' } }

  before do
    Time.stubs(:now).returns(now)
  end

  [:debug, :info, :warn, :error, :fatal].each do |level|
    it "logs #{level}" do
      expect { subject.send(level, message) }.to output(json_matching(level.to_s.upcase, message)).to_stdout_from_any_process
    end
  end

  it 'logs unknown' do
    expect { subject.unknown(message) }.to output(json_matching("ANY", message)).to_stdout_from_any_process
  end

  it 'only accepts hashes' do
    expect { subject.unknown("just a string!") }.to raise_error(ArgumentError, /must be a Hash/)
  end

  context 'logging a hash as a message' do
    it 'merges the contents' do
      input = {
          additional_field: "some value"
      }
      expected = {
          severity: 'DEBUG',
          time: now,
          additional_field: "some value"
      }

      expect { subject.debug(input) }.to output(as_regex(expected)).to_stdout_from_any_process
    end
  end

  def json_matching(level, message)
    contents = {
        severity: level,
        time: now
    }.merge(message)

    as_regex(contents)
  end

  def as_regex(contents)
    /#{Regexp.quote(contents.to_json)}/
  end
end