Create tests
Last modified: 09 August 2022RubyMine provides the ability to create missing tests in various ways. For example, you can:
create tests from templates
create a test when navigating to it from a test subject
generate tests using Rails generators
Create tests from a template
To create a new test from a template, do the following:
Do one of the following:
In the Project tool window Alt+1, select the directory in which you want to create a new file, and then choose File | New from the main menu.
Right-click the corresponding directory and select New from the context menu.
Press Alt+Insert.
note
Note that the target directory should be marked as a Test Sources Root.
Select Ruby Test from the list and press Enter.
In the New Ruby Test popup, you need to select the testing framework and specify the class name to be tested. For example, do the following to create a new RSpec test:
Select RSpec.
Specify the described class name (for example,
User
) and press Enter.
RubyMine will create a test file with corresponding initial content and open it in the editor. Here you can add the required code.
If necessary, you can modify predefined templates in Settings/Preferences Ctrl+Alt+S on the Editor | File and Code Templates page. Learn more at File templates.
Create a test when navigating to it
RubyMine provides the capability to create a test when navigating to it from a class that needs to be tested.
In the editor, open the required class.
From the main menu, select Navigate | Test or press Ctrl+Shift+T.
If the corresponding test doesn't exist, the Create Test popup appears.
In this popup, you can select one of the following:
Create New Test: Create a new test for a class, replicating the directory structure based on the path to the test subject.
Run 'rails generate': Generate a new test for a class using Rails generators.
Choose Create New Test.
In the Choose Destination Directory dialog, choose the required folder and click OK.
In the invoked popup, choose whether to create a test from scratch or use a predefined test template.
Press Enter. The IDE will create the test under the necessary directory, replicating the directory structure based on the path to the test subject.
Generate tests for Rails applications
RubyMine provides several ways to generate missing tests for Rails elements (controllers, models, and so on).
Create tests using Rails generators
You can use Rails generators supplied with a testing framework to create tests. For example, you can create a model test using the rspec:model
generator for RSpec or minitest:model
for Minitest. To do this in RubyMine:
From the main menu, select Tools | Run Rails Generator (Ctrl+Alt+G).
In the invoked popup, start typing the required generator name. For example, to create an RSpec model test start typing rspec:model and then select
rails g rspec:model
. Press Enter.Specify the generator arguments. For example, for the User model we pass
user
as an argument. Click OK.
Create tests alongside Rails elements
Rails allows you to generate tests alongside other applications elements such as controllers, models, and so on. Learn how to create Rails application elements in RubyMine from Create Rails application elements.
tip
See Configuring Generators to learn how to configure the desired test framework.
Thanks for your feedback!