Logo

TODAY I LEARNED

Test Rails log messages with RSpec

Have you wondered how to test your Rails log messages?

Try this:

it "logs a message" do
  allow(Rails.logger).to receive(:info)
  expect(Rails.logger).to receive(:info).with("Someone read this post!")

  visit root_path

  expect(page).to have_content "Welcome to TIL"
end

Or this:

it "logs a message" do
  allow(Rails.logger).to receive(:info)

  visit root_path

  expect(page).to have_content "Welcome to TIL"
  expect(Rails.logger).to have_received(:info).with("Someone read this post!")
end

Source

Learned by Victor Velazquez on Feb 18, 2021

973983 adforcodereview v3 0211021 c02 021121