RubyMine 2017.1 Help

RSpec 'let'

On this page:

Basics

RubyMine implements extracting let as follows:

  • Introduces the initialization of let inside rspec describe/context block as the first statement.
  • Provides an editable template to insert the new name.
  • Searches for fragments in the scenarios and replaces them with the let calls.
  • If a fragment has been found in several scenarios(see example 3), suggests a confirmation dialog:
    /help/img/idea/2017.1/rm_extract_let_confirmation.png

Performing refactoring

To perform the refactoring, follow these steps:

  1. Open for editing an RSpec file in question.
  2. Make the required selection.
  3. Do one of the following:
    • On the main menu, choose Refactor | Extract | RSpec 'let'.
    • On the context menu of the selection, choose Refactor | Extract | RSpec 'let'.
    • Press Ctrl+Shift+Alt+R.

As a results, a new let block shows up in the current RSpec block.

Examples

BeforeAfter
RSpec.describe "let" do it "memorizes the value 2" do # selection starts here # count = 1 count1 = count # selection ends here # expect(count).to eq(1) expect(count1).to eq(1) end end
RSpec.describe "let" do let(:name) do count = 1 count1 = count return count, count1 end it "memorizes the value 2" do count, count1 = name expect(count).to eq(1) expect(count1).to eq(1) end end
RSpec.describe "let" do it "memorizes the value" do count = 1 count.should == 1 end end
RSpec.describe "let" do let(:count) do count = 1 end it "memorizes the value" do count.should == 1 end end
RSpec.describe "let" do it "memorizes the value" do count = 1 count.should == 1 end it "memorizes the value" do count = 1 count.should == 1 end it "memorizes the value" do count = 1 count.should == 1 end end
RSpec.describe "let" do let(:count) do count = 1 end it "memorizes the value" do count.should == 1 end it "memorizes the value" do count.should == 1 end it "memorizes the value" do count.should == 1 end end

See Also

Language and Framework-Specific Guidelines:

Last modified: 18 July 2017