RSpec: how to test Rails logger message expectations?

后端 未结 6 1052
囚心锁ツ
囚心锁ツ 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:09

    Even I had very similar error:

    Failure/Error: expect(Rails.logger).to receive(:info).with("some message")
           (#).info("some message")
               expected: 1 time with arguments: ("some message")
               received: 0 times
    

    The below worked for me,

    expect { my_method }.
                to output(/error messsage/).to_stdout_from_any_process
    

    Reference: https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher

提交回复
热议问题