While I was writing RSpec, I had to spend some time searching for "Which product and which Spec did I write for a similar comparison?", So I will summarize it here as an article. .. Hope it helps you and other developers in the future.
it 'Error log is output' do
expect(Rails.logger).to receive(:error).with(/code: 400/)
#Below, write the process you want to test
end
As a point
--Write expect at the very beginning of it. Otherwise, the log output method will not be captured by receive.
--Write the message you want to be included in with
. If you write it as it is, it will not pass unless it is an exact match, so you can write a case that contains a specific message by enclosing it in a slash. The so-called regular expression.
it 'No error log is output' do
expect(Rails.logger).to receive(:error).exactly(0).times
#Below, write the process you want to test
end
In the application of the previous example, I also wanted to check "In the case where the process is completed normally, no error log should be output."
Since you can specify the number of times by connecting to the receive
method, I confirmed that the method was not called in .exactly (0) .times
.
Referenced site
People who often write Ruby and Rails may say, "It's natural!", But I've been assigned to full-scale development using Rails for the first time recently, and I'm confused. I got it when I was developing it, so I hope you can see it with a gentle eye. I will add it when something that seems to be usable comes out.
Thank you very much.
Recommended Posts