# RSpec

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com/help/ruby/2026.3/llms.txt).

[RSpec](https://github.com/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

Rails:

Procedure:

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

2. Press `Ctrl` (Windows), `Ctrl` (macOS), `Ctrl` (IntelliJ IDEA Classic (macOS)), `Ctrl` (macOS System Shortcuts), `Ctrl` (XWin), `Ctrl` (GNOME), `Ctrl` (KDE), `Ctrl` (Emacs), `Ctrl` (Sublime Text), `Ctrl` (Sublime Text (macOS)), `Ctrl` (Visual Studio), `Ctrl` (Visual Studio (macOS)), `Ctrl` (TextMate), `Ctrl` (NetBeans), `Ctrl` (Eclipse), `Ctrl` (Eclipse (macOS)) twice to open the Run anything popup. Use the `rails generate rspec:install` command to initiate RSpec and create necessary files and directories.

Ruby:

Procedure:

1. Add the `rspec` gem to your `Gemfile`.

2. Press `Ctrl` (Windows), `Ctrl` (macOS), `Ctrl` (IntelliJ IDEA Classic (macOS)), `Ctrl` (macOS System Shortcuts), `Ctrl` (XWin), `Ctrl` (GNOME), `Ctrl` (KDE), `Ctrl` (Emacs), `Ctrl` (Sublime Text), `Ctrl` (Sublime Text (macOS)), `Ctrl` (Visual Studio), `Ctrl` (Visual Studio (macOS)), `Ctrl` (TextMate), `Ctrl` (NetBeans), `Ctrl` (Eclipse), `Ctrl` (Eclipse (macOS)) 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](#new_rspec_test_from_template) or [use framework-specific Rails generators](#new_rspec_test_using_rails_generators) in case you're developing a Rails application.

Procedure: Create an RSpec test from a template

1. Do one of the following:

* In the [Project](project-tool-window.html) tool window (`Alt+1` (Windows), `⌘ 1` (macOS), `⌘ 1` (IntelliJ IDEA Classic (macOS)), `⌘ 1` (macOS System Shortcuts), `Alt+1` (XWin), `Alt+1` (GNOME), `Alt+1` (KDE), `Alt+1` (Emacs), `Alt+1` (Sublime Text), `⌘ 1` (Sublime Text (macOS)), `Ctrl+Alt+L` (Visual Studio), `⌘ ⌥ L` (Visual Studio (macOS)), `Meta+1` (TextMate), `Ctrl+1` (NetBeans), `Alt+1` (Eclipse), `Alt+1` (Eclipse (macOS))), 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` (Windows), `⌘ N` (macOS), `⌃ N` (IntelliJ IDEA Classic (macOS)), `⌘ N` (macOS System Shortcuts), `Alt+Insert` (XWin), `Alt+Insert` (GNOME), `Alt+Insert` (KDE), `Alt+Insert` (Emacs), `Alt+Insert` (Sublime Text), `⌘ N` (Sublime Text (macOS)), `Alt+Insert` (Visual Studio), `⌘ ⌃ N` (Visual Studio (macOS)), `Ctrl+N` (TextMate), `Alt+Insert` (NetBeans), `Alt+Insert` (Eclipse), `⌘ N` (Eclipse (macOS)).

2. Select Ruby Test from the list and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

3. In the New Ruby Test popup, select RSpec, specify the described class name, and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

![Creating a new RSpec test](https://resources.jetbrains.com/help/img/idea/2026.3/rm_create_tests_new_rspec_popup.png)

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](https://resources.jetbrains.com/help/img/idea/2026.3/rm_create_tests_rspec_template_example.png)

> **Tip:**
> If necessary, you can modify predefined templates in Settings `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Meta+Comma` (TextMate), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS)) on the `Editor | File and Code Templates` page. Learn more in [File templates](using-file-and-code-templates.html).

> **Note:**
> Before creating new RSpec tests in RubyMine, make sure that the directory dedicated for storing tests is [marked
> as a Test Sources Root](create-tests.html#mark_test_sources_root).

Procedure: Create an RSpec test using Rails generators

1. Press `Ctrl` (Windows), `Ctrl` (macOS), `Ctrl` (IntelliJ IDEA Classic (macOS)), `Ctrl` (macOS System Shortcuts), `Ctrl` (XWin), `Ctrl` (GNOME), `Ctrl` (KDE), `Ctrl` (Emacs), `Ctrl` (Sublime Text), `Ctrl` (Sublime Text (macOS)), `Ctrl` (Visual Studio), `Ctrl` (Visual Studio (macOS)), `Ctrl` (TextMate), `Ctrl` (NetBeans), `Ctrl` (Eclipse), `Ctrl` (Eclipse (macOS)) twice to invoke the Run Anything popup.

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` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

![Create a new RSpec model test](https://resources.jetbrains.com/help/img/idea/2026.3/rm_rails_generators_new_test.png)

3. Specify the generator arguments. For example, for the User model we pass `user` as an argument. Click OK.

![Add New RSpec:model](https://resources.jetbrains.com/help/img/idea/2026.3/rm_create_tests_add_new_rspec_dialog.png)

## 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](https://resources.jetbrains.com/help/img/idea/2026.3/rm_rspec_completion.png)

## Navigation

Procedure: Navigate to a test subject

* You can navigate from an RSpec test to its test subject quickly by pressing `Ctrl+Shift+T` (Windows), `⌘ ⇧ T` (macOS), `⌘ ⇧ T` (IntelliJ IDEA Classic (macOS)), `⌃ ⇧ T` (macOS System Shortcuts), `Ctrl+Shift+T` (XWin), `Ctrl+Shift+T` (GNOME), `Ctrl+Shift+T` (KDE), `Ctrl+Shift+T` (Emacs), `Ctrl+Shift+T` (Sublime Text), `Ctrl+Shift+T` (Sublime Text (macOS)), `Ctrl+Shift+T` (Visual Studio), `Ctrl+Shift+T` (Visual Studio (macOS)), `Ctrl+Alt+T` (TextMate), `Ctrl+Shift+T` (NetBeans), `Ctrl+Shift+T` (Eclipse), `Ctrl+Shift+T` (Eclipse (macOS)) or selecting `Navigate | Test Subject` from the main menu.

[Video](https://resources.jetbrains.com/help/img/idea/2026.3/rm_rspec_navigate_to_test_subject.mp4)

Procedure: 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` (Windows), `⌘ B` (macOS), `⌘ B` (IntelliJ IDEA Classic (macOS)), `⌘ B` (macOS System Shortcuts), `Ctrl+B` (XWin), `Ctrl+B` (GNOME), `Ctrl+B` (KDE), `Ctrl+Alt+G` (Emacs), `F12` (Sublime Text), `F12` (Sublime Text (macOS)), `F12` (Visual Studio), `F12` (Visual Studio (macOS)), `Meta+B` (TextMate), `Ctrl+B` (NetBeans), `F3` (Eclipse), `F3` (Eclipse (macOS)).

* Alternatively, use `Ctrl+Click` (Windows), `Ctrl+Click` (macOS), `Ctrl+Click` (IntelliJ IDEA Classic (macOS)), `Ctrl+Click` (macOS System Shortcuts), `Ctrl+Click` (XWin), `Ctrl+Click` (GNOME), `Ctrl+Click` (KDE), `Ctrl+Click` (Emacs), `Ctrl+Click` (Sublime Text), `Ctrl+Click` (Sublime Text (macOS)), `Ctrl+Click` (Visual Studio), `Ctrl+Click` (Visual Studio (macOS)), `Ctrl+Click` (TextMate), `Ctrl+Click` (NetBeans), `Ctrl+Click` (Eclipse), `Ctrl+Click` (Eclipse (macOS)): keeping `Ctrl` (Windows), `Ctrl` (macOS), `Ctrl` (IntelliJ IDEA Classic (macOS)), `Ctrl` (macOS System Shortcuts), `Ctrl` (XWin), `Ctrl` (GNOME), `Ctrl` (KDE), `Ctrl` (Emacs), `Ctrl` (Sublime Text), `Ctrl` (Sublime Text (macOS)), `Ctrl` (Visual Studio), `Ctrl` (Visual Studio (macOS)), `Ctrl` (TextMate), `Ctrl` (NetBeans), `Ctrl` (Eclipse), `Ctrl` (Eclipse (macOS)) 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.

[Video](https://resources.jetbrains.com/help/img/idea/2026.3/rm_rspec_go_to_usage.mp4)

## 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](rake.html) tasks.

Procedure: 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](https://resources.jetbrains.com/help/img/idea/2026.3/ruby_runAllTests.png)

Procedure: Run RSpec tests from the editor

* Open the necessary RSpec file and click the ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.run.svg) gutter icon next to the test you want to run.

* Alternatively, place the caret at the desired test, press `Alt+Enter` (Windows), `⌥ ⏎` (macOS), `⌥ ⏎` (IntelliJ IDEA Classic (macOS)), `⌥ ⏎` (macOS System Shortcuts), `Alt+Enter` (XWin), `Alt+Enter` (GNOME), `Alt+Enter` (KDE), `Alt+Enter` (Emacs), `Alt+Enter` (Sublime Text), `⌥ ⏎` (Sublime Text (macOS)), `Alt+Enter` (Visual Studio), `⌥ ⏎` (Visual Studio (macOS)), `Alt+Enter` (TextMate), `Alt+Enter` (NetBeans), `Ctrl+1` (Eclipse), `⌘ 1` (Eclipse (macOS)), select the required run action, and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

* If you want to run all tests related to a described test subject, click the ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.run_run.svg) 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](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.green2.svg) for successful tests or ![the Rerun the Run button](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.rerunFailedTests.svg) for failed tests.

* When running a shared example, RubyMine suggests selecting a context for executing this example. Click the ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.runConfigurations.testState.run_run.svg) 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.

Procedure: Run RSpec tests using a Rake task

* Press `Ctrl` (Windows), `Ctrl` (macOS), `Ctrl` (IntelliJ IDEA Classic (macOS)), `Ctrl` (macOS System Shortcuts), `Ctrl` (XWin), `Ctrl` (GNOME), `Ctrl` (KDE), `Ctrl` (Emacs), `Ctrl` (Sublime Text), `Ctrl` (Sublime Text (macOS)), `Ctrl` (Visual Studio), `Ctrl` (Visual Studio (macOS)), `Ctrl` (TextMate), `Ctrl` (NetBeans), `Ctrl` (Eclipse), `Ctrl` (Eclipse (macOS)) 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` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

![Run RSpec using Rake](https://resources.jetbrains.com/help/img/idea/2026.3/rm_rspec_run_rake_task.png)

When you run RSpec tests using a context menu or from the editor, RubyMine automatically creates a corresponding RSpec [temporary configuration](run-debug-configuration.html#temp_config), which can be saved. If necessary, you can create an RSpec run/debug configuration [manually from the predefined template](run-debug-configuration.html#createExplicitly).

For more information, refer to [Run/Debug Configuration: RSpec](run-debug-configuration-rspec.html).

## 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:

Procedure:

1. In a spec file, select a required code fragment and select `Refactor | Refactor This | RSpec 'let'` (`⌘ ⇧ L` (Windows), `⌘ ⇧ L` (macOS), `⌘ ⇧ L` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ ⌥ L` (macOS System Shortcuts), `⌘ ⇧ L` (XWin), `⌘ ⇧ L` (GNOME), `⌘ ⇧ L` (KDE), `⌘ ⇧ L` (Emacs), `⌘ ⇧ L` (Sublime Text), `⌘ ⇧ L` (Sublime Text (macOS)), `⌘ ⇧ L` (Visual Studio), `⌘ ⇧ L` (Visual Studio (macOS)), `⌘ ⇧ L` (TextMate), `⌘ ⇧ L` (NetBeans), `⌘ ⇧ L` (Eclipse), `⌘ ⇧ L` (Eclipse (macOS))) from the main menu.

![Extract method](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_rspec_let.png)

2. Specify the desired name of a helper method and press `Enter` (Windows), `⏎` (macOS), `⏎` (IntelliJ IDEA Classic (macOS)), `⏎` (macOS System Shortcuts), `Enter` (XWin), `Enter` (GNOME), `Enter` (KDE), `Enter` (Emacs), `Enter` (Sublime Text), `⏎` (Sublime Text (macOS)), `Enter` (Visual Studio), `⏎` (Visual Studio (macOS)), `Enter` (TextMate), `Enter` (NetBeans), `Enter` (Eclipse), `⏎` (Eclipse (macOS)).

![Extract method](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_rspec_let_name.png)

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](https://resources.jetbrains.com/help/img/idea/2026.3/rm_refactoring_extract_rspec_let_replace.png)

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.

### Example

Before:

```RUBY
describe "GetTime" do
    it "gets the same time" do
        puts Time.now
        sleep(3)
        puts Time.now
    end
end
```

After:

```RUBY
describe "GetTime" do
    let(:current_time) { Time.now }

    it "gets the same time" do
        puts current_time
        sleep(3)
        puts current_time
    end
end
```

## See also

### Procedures

[Test](testing-frameworks.html) [Extract RSpec 'let'](rspec-let.html)

### Reference

[Run/Debug Configuration: RSpec](run-debug-configuration-rspec.html)

### External Links

[http://rspec.info/](http://rspec.info/)

