如何在特定线路上 运行 进行 RSpec 测试?

How do I run an RSpec test on a specific line?

我有下一个规范文件:

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

我想 运行 从第 16 行开始测试(不是整个测试),所以我输入:

rspec spec/models/orders_spec.rb -l16

我没有得到 运行ning 测试,而是得到下一个错误:

invalid option: -l18

如何运行从某一行开始测试?

您需要使用格式 rspec path/to/spec.rb:line_no

(即)rspec spec/models/orders_spec.rb:16

这里是 link 到 RelishApp(RSpec 文档的最佳位置),如果您想阅读更多内容。

使用行号的问题在于,当您在它之前添加或删除行时,它可能会在调试过程中发生变化。

使用 rspec 执行此操作的更可预测的方法是使用 -e <example name>。在这种情况下:

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