A collection of RSpecs that I used frequently

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.

Confirm that the error log is output.


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.

Referenced site

Confirm that the error log is not output.

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

A collection of RSpecs that I used frequently
A collection of commands that were frequently used on heroku
It's just now, but a collection of commands that frequently appear in Rails
Frequently used Maven command collection
Generate a unique collection of values from a collection that contains duplicate values
A collection of Eclipse shortcuts that new graduates find useful
Summary of frequently used Docker commands
I made a question that can be used for a technical interview
A collection of phrases that impresses the "different feeling" of Java and JavaScript
[Rails] A collection of tips that are immediately useful for improving performance
Frequently used functions and commands of Xcode
A collection of methods often used when manipulating time with TimeWithZone of Rails
[Note] A list of commands that I checked many times after trying Ruby.
[Android] I want to create a ViewPager that can be used for tutorials
I made a THETA API client that can be used for plug-in development
java stream A memorandum of intermediate / termination operations that are really often used 1
A simple example of a Servlet that displays Japanese
I made a viewer app that displays a PDF
A description of Interface that is often mistaken
[Ruby] I want to make a program that displays today's day of the week!
I hate this kind of code! A collection of anti-patterns actually seen in the field
Minecraft Modding's recommendation "I'm glad I did Modding and a collection of useful sites"
A collection of methods that replace characters entered in hashtag search with a nice feeling
I made a Docker image of SDAPS for Japanese
Summary of frequently used commands in Rails and Docker
A review of the code used by rails beginners
I want to call a method of another class
List of things I used without understanding well: Ruby
I made a library that works like a Safari tab !!
[Rails] Volume that displays favorites and a list of favorites
I wrote a sequence diagram of the j.u.c.Flow sample
Builder pattern that forces a set of required properties
I made a Wrapper that calls KNP from Java
List of frequently used Java instructions (for beginners and beginners)
I learned about the existence of a gemspec file
I have a question about keyboard input of numbers
A note that I had trouble when trying to use nginx with Remote-Containers of vscode
I tried a puzzle that can only be solved by the bottom 10% of bad engineers