【问题标题】:How do I run an RSpec test on a specific line?如何在特定线路上运行 RSpec 测试?
【发布时间】:2015-05-02 20:26:06
【问题描述】:

我有下一个规格文件:

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

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

invalid option: -l18

如何从某一行运行测试?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rspec


    【解决方案1】:

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

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

    如果您想阅读更多内容,请访问RelishApp(RSpec 文档的最佳位置)的链接。

    【讨论】:

    • 谢谢!我在截屏视频中看到了带有“-l”的方法,但它不适用于我的环境。
    • 很高兴为您提供帮助!看起来 -l--line 在 RSpec 的 3.x 版本中被删除了。
    【解决方案2】:

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

    使用 rspec 更可预测的方法是使用 -e &lt;example name&gt;。在这种情况下:

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

    【讨论】:

      猜你喜欢
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      • 2014-02-03
      相关资源
      最近更新 更多