RubyMine 2024.2 Help

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

  1. Add the rspec-rails gem to your Gemfile.

  2. 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.

  1. Add the rspec gem to your Gemfile.

  2. Press Ctrl 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

  1. 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 directory and select New from the context menu.

    • Select the directory and press Alt+Insert.

  2. Select Ruby Test from the list and press Enter.

  3. In the New Ruby Test popup, select RSpec, specify the described class name, and press Enter.

    Creating a new RSpec test

    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.

    RSpec template editor

Create an RSpec test from a test subject

  1. Open the required class in the editor.

  2. Go to Navigate | Test or press Ctrl+Shift+T.

    If the corresponding test doesn't exist, the Create Test popup appears.

    Create Test popup

    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.

  3. Select the Create New Test option.

  4. In the Choose Destination Directory dialog, choose the spec folder and click OK.

    Choose Destination Directory
  5. In the invoked popup, select the RSpec template and press Enter.

    New Test popup

    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

  1. Go to Tools | Run Rails Generator (Ctrl+Alt+G).

  2. 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.

    Create a new RSpec model test
  3. Specify the generator arguments. For example, for the User model we pass user as an argument. Click OK.

    Add New RSpec:model

Code completion

RubyMine provides code completion for class names, variables, methods, shared examples and contexts, and more in RSpec files.

Code completion in RSpec files

Navigation

Navigate to a test subject

  • You can navigate from an RSpec test to its test subject quickly by pressing Ctrl+Shift+T or selecting Navigate | Test Subject 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 Ctrl+B.

    • Alternatively, use Ctrl+Click: 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.

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 tests from Project view

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: the Run test icon for successful tests or the Rerun the Run button 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 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.

    Run RSpec using Rake

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.

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:

  1. In a spec file, select a required code fragment and select Refactor | Extract/Introduce | RSpec 'let' ⌘ ⇧ L from the main menu.

  2. Specify the desired name of a helper method and press Enter.

  3. If more than one occurrence of the code fragment is found, RubyMine will suggest replacing these occurrences with a helper method call.

    Extract method

    Click Yes to replace the found occurrences. If you want to replace the selected occurrence only, click No.

Example

describe "GetTime" do it "gets the same time" do puts Time.now sleep(3) puts Time.now end end
describe "GetTime" do let(:current_time) { Time.now } it "gets the same time" do puts current_time sleep(3) puts current_time end end
Last modified: 28 June 2024