Test Rails log messages with RSpec

Have you wondered how to test your Rails log messages?

Try this:

```ruby
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:

```ruby
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](https://everydayrails.com/2020/08/10/rails-log-message-testing-rspec.html)



Victor Velazquez
February 18, 2021
