7

I have next spec file:

require 'rails_helper'

describe Order do
  it "calculates the total price of the order" do
    item1 = create(:item)
    item2 = create(:item, price: 20)

    order = create(:order)
    order.items << item1
    order.items << item2

    order.calculate_total
    expect(order.total).to eq(30)
  end

  it "raises exception if order has no items in it" do
    expect { create(:order) }.to raise_exception
  end
end

And I want to run test from 16 line (not entire test), so I type:

rspec spec/models/orders_spec.rb -l16

Instead of get running test, i get next error:

invalid option: -l18

How to run test from a certain line?

Kavachaj
  • 411
  • 1
  • 4
  • 15

3 Answers3

13

You'll want to use the format rspec path/to/spec.rb:line_no.

(i.e.) rspec spec/models/orders_spec.rb:16

Here's a link to RelishApp (the best location for the RSpec documentation) if you'd like some more reading.

Taylor Glaeser
  • 1,338
  • 12
  • 18
3

The problem with using line number is that it might change during your debugging when you add or remove lines before it.

The more predictable way to do this with rspec is using -e <example name>. In this case:

rspec -e "raises exception if order has no items in it"

Dave Slutzkin
  • 1,592
  • 14
  • 19
0

If you need run RSpec at a specific line number you can use colon

Here RSpec will run only the tests on line 20:

rspec spec/models/user_spec.rb:20

For multiple tests use multiple colons

Here RSpec will run only the tests on lines 20, 26, 37

rspec spec/models/user_spec.rb:20:26:37
mechnicov
  • 12,025
  • 4
  • 33
  • 56