RSpec: how to test Rails logger message expectations?

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

    If you want to keep consistency in your tests, but setting the expectations at last you need to add in your setup:

    setup do
     allow(Rails.logger).to receive(:info)
    end
    ...
    
    it 'should log an info message' do
     {code}
    
      expect(Rails.logger).to have_received(:info).with('Starting the worker...')
    end
    
    

提交回复
热议问题