RSpec: how to test Rails logger message expectations?

后端 未结 6 1045
囚心锁ツ
囚心锁ツ 2020-12-24 00:17

I am trying to test that the Rails logger receives messages in some of my specs. I am using the Logging gem.

Let\'s say that I have a class like this:



        
6条回答
  •  盖世英雄少女心
    2020-12-24 01:02

    If your goal is to test logging functionality you may also consider verifying the output to standard streams.

    This will spare you the mocking process and test whether messages will actually end up where they supposed to (STDOUT/STDERR).

    With RSpec's output matcher (introduced in 3.0) you can do the following:

    expect { my_method }.to output("my message").to_stdout
    expect { my_method }.to output("my error").to_stderr
    

    In case of libraries such as Logger or Logging you may have to use output.to_<>_from_any_process.

提交回复
热议问题