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:
With RSpec 3+ version
Actual code containing single invocation of Rails.logger.error:
Rails.logger.error "Some useful error message"
Spec code:
expect(Rails.logger).to receive(:error).with(/error message/)
If you want the error message to be actually logged while the spec runs then use following code:
expect(Rails.logger).to receive(:error).with(/error message/).and_call_original
Actual code containing multiple invocations of Rails.logger.error:
Rails.logger.error "Technical Error Message"
Rails.logger.error "User-friendly Error Message"
Spec code:
expect(Rails.logger).to receive(:error).ordered
expect(Rails.logger).to receive(:error).with(/User-friendly Error /).ordered.and_call_original
Also if you care about just matching the first message and not any subsequent messages then you can use following
expect(Rails.logger).to receive(:debug).with("Technical Error Message").ordered.and_call_original
expect(Rails.logger).to receive(:debug).at_least(:once).with(instance_of(String)).ordered
Note in above variation setting .ordered is important else expectations set start failing.
References:
http://www.relishapp.com/rspec/rspec-mocks/v/3-4/docs/setting-constraints/matching-arguments
http://www.relishapp.com/rspec/rspec-mocks/v/3-4/docs/setting-constraints/message-order