RSpec
RSpec is a popular behavior-driven development (BDD) framework for testing Ruby/Rails applications. RubyMine comes with a number of RSpec support features and allows you to run tests, use coding assistance, navigation, refactorings, and many more.
Enable RSpec support
Add the rspec-rails gem to your Gemfile.
Press Control twice to open the Run anything popup. Use the
rails generate rspec:install
command to initiate RSpec and create necessary files and directories.
Add the rspec-rails gem to your Gemfile.
Press Control twice to open the Run anything popup. Use the
rspec --init
command to initiate RSpec and create necessary files and directories.
Create an RSpec test
RubyMine provides multiple ways to create Rspec tests. You can create RSpec tests from a dedicated template, generate them from classes that need to be tested, or use framework-specific Rails generators in case you're developing a Rails application.
Create an RSpec test from a template
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 from the main menu.
Right-click the corresponding directory and select New from the context menu.
Press Alt+Insert.
Select Ruby Test from the list and press Enter.
In the New Ruby Test popup, select RSpec, specify the described class name, and press Enter.
RubyMine will create a test file with the initial content and open it in the editor. Use this file as a template and add the required code.
Create an RSpec test from a test subject
Open the required class in the editor.
From the main menu, select Navigate | Test or press Control+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.
Select the Create New Test option.
In the Choose Destination Directory dialog, choose the spec folder and click OK.
In the invoked popup, select the RSpec template and press Enter.
RubyMine will create a new RSpec test under the necessary directory replicating the directory structure based on the path to the test subject. For example, if you're creating a model test, the test will be stored under the spec/models directory.
Create an RSpec test using Rails generators
From the main menu, select
(Control+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.
Code completion
RubyMine provides code completion for class names, variables, methods, shared examples and contexts, and more in RSpec files.
Navigation
Navigate to a test subject
You can navigate from an RSpec test to its test subject quickly by pressing Control+Shift+T or selecting
from the main menu.
Go to a declaration or usage
RubyMine supports navigation between declarations and usages of factories, fixtures, shared examples, and shared contexts used in your test.
Do one of the following:
Place your caret at the desired symbol in your RSpec test and press Control+B.
Alternatively, use Control+LeftClick: keeping Control pressed, hover your mouse pointer over the symbol. When the symbol turns into a hyperlink, its declaration will be displayed in the tooltip. Click the hyperlink without releasing the key to open the declaration in the editor.
Run RSpec tests
RubyMine allows you to run individual RSpec tests or all tests from the specified folder or file. You can also run tests using Rake tasks.
Run multiple RSpec tests from a folder
In the Project view, right-click the desired folder and select Run 'All features in ...'.
Run RSpec tests from the editor
Open the necessary RSpec file and click the gutter icon next to the test you want to run.
Alternatively, place the caret at the desired test, press Alt+Enter, select the required run action, and press Enter.
If you want to run all tests related to a described test subject, click the gutter icon next to the
describe
method in your file and select Run 'RSpec: ...'.You can re-run a particular test using the gutter icon indicating its state: for successful tests or for failed tests.
When running a shared example, RubyMine suggests selecting a context for executing this example. Click the gutter icon next to the declaration of a desired shared example and select Run 'RSpec: ...'. In the Run Example Groups popup that appears, select the necessary context to execute the shared example.
Run RSpec tests using a Rake task
Press Control twice to invoke the Run Anything popup. Start typing the required task name (it will start with
rake spec
), select it from the list and press Enter.
When you run RSpec tests using a context menu or from the editor, RubyMine automatically creates a corresponding RSpec temporary configuration, which can be saved. If necessary, you can create an RSpec run/debug configuration manually from the predefined template.
For more details, refer to Run/Debug Configuration: RSpec.
Extract RSpec 'let' refactoring
The Extract RSpec 'let' refactoring allows you to extract a specified code fragment into a memoized helper method. To do this, perform the following steps:
In a spec file, select a required code fragment and select
Command Shift L from the main menu.Specify the desired name of a helper method and press Enter.
If more than one occurrence of the code fragment is found, RubyMine will suggest replacing these occurrences with a helper method call.
Click Yes to replace the found occurrences. If you want to replace the selected occurrence only, click No.