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.
Add the rspec-rails gem to your Gemfile.
Press Ctrl 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 gem to your Gemfile.
Press Ctrl twice to open the Run anything popup. Use the
rspec --init
command to initiate RSpec and create necessary files and directories.
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.
Do one of the following:
In the Project tool window (Alt01), select the directory in which you want to create a new file, and then choose File | New from the main menu.
Right-click the directory and select New from the context menu.
Select the directory and press AltInsert.
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.
tip
If necessary, you can modify predefined templates in Settings CtrlAlt0S on the Editor | File and Code Templates page. Learn more at File templates.
note
Before creating new RSpec tests in RubyMine, make sure that the directory dedicated for storing tests is marked as a Test Sources Root.
Open the required class in the editor.
Go to Navigate | Test or press CtrlShift0T.
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.
Go to Tools | Run Rails Generator (CtrlAlt0G).
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.
RubyMine provides code completion for class names, variables, methods, shared examples and contexts, and more in RSpec files.

You can navigate from an RSpec test to its test subject quickly by pressing CtrlShift0T or selecting Navigate | Test Subject from the main menu.
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 Ctrl0B.
Alternatively, use CtrlClick: keeping Ctrl pressed, hover 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.
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.
In the Project view, right-click the desired folder and select Run 'All features in ...'.
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 AltEnter, 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.
Press Ctrl 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 information, refer to Run/Debug Configuration: RSpec.
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 Refactor | Extract/Introduce | RSpec 'let' 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.
note
If you click Yes, RubyMine suggests replacing the found occurrences one by one or all at a time.
Before
describe "GetTime" do it "gets the same time" do puts Time.now sleep(3) puts Time.now endend
After
describe "GetTime" do let(:current_time) { Time.now } it "gets the same time" do puts current_time sleep(3) puts current_time endend
Thanks for your feedback!