RubyMine provides the ability to create missing tests in various ways, for example, you can:
create tests from templates
generate tests using Rails generators
create a test when navigating to it from a test subject
Create a test from 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 on the main menu.
Right-click the corresponding directory and select New from the context menu.
Select the desired test template from the list, for instance, RSpec Template.
In the opened dialog, specify the required names. For example, for RSpec Template you need to specify the file and behavior names. Click OK.
RubyMine will create a test file with corresponding initial content and open it in the editor. Here you can add the required code.
Generate tests for Rails applications
RubyMine provides several ways to generate missing tests for Rails elements (controllers, models, and so on).
Create a test when navigating to it
In the editor, open the required Rails element, for example, the controller or model.
From the main menu, select Navigate | Test or press Ctrl+Shift+T.
If the corresponding test doesn't exist, the Generate Test popup appears. Select the required testing framework and press Enter.
(Optional) If the related testing framework is missing in the project’s SDK, RubyMine will suggest to install the missing gem (for example, 'rspec-rails').
Click Bundle it and then click Install in the opened Bundle Install dialog.
The IDE will install the missing gem and generate the test under the necessary directory (for example, spec or test depending on the selected testing framework).
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 Creating Rails Application Elements.
Create RSpec tests using Rails generators
RSpec generators can be run independently to create tests. You can do this as follows:
From the main menu, select(Ctrl+Alt+G).
In the invoked popup, start typing the required generator name. For example,
rspec:modelin the image below will be used to create a model test.
Specify generator argumens. For example, for the User model we pass
useras an argument. Click OK.