ChatGPT解决这个技术问题 Extra ChatGPT

在 rspec 中禁用一组测试?

我有一个测试规范,其中 describes 一个类,其中有各种 contexts,每个都有各种 it 块。

有没有办法可以暂时禁用 context

我尝试在要禁用的 context 内的最顶部添加一个 pending "temporarily disabled" 调用,当我运行规范时我确实看到了一些关于挂起的信息,但它只是继续运行其余的测试。

这就是我所拥有的:

describe Something
  context "some tests" do
    it "should blah" do
      true
    end
  end

  context "some other tests" do
    pending "temporarily disabled"

    it "should do something destructive" do
      blah
    end
  end
end

但就像我说的那样,它只是继续在挂起的调用下运行测试。

搜索使我找到了这个 mailing list thread,其中 rspec 的创建者 (?) 说这在我正在运行的 rspec 2 中是可能的。我想它确实有效,但它没有达到禁用以下所有测试的预期效果,这是我看到 pending 调用时的想法。

有替代方案还是我做错了?


Y
Yaro Holodiuk

要使用 RSpec 3 禁用规范树,您可以:

before { skip }
# or 
xdescribe
# or 
xcontext

您可以添加一条带有跳过的消息,该消息将显示在输出中:

before { skip("Awaiting a fix in the gem") }

使用 RSpec 2:

before { pending }

您如何在具有以下内容的块上做到这一点:describe 'XXXXX' do .... end
@p.matsinopoulos 只需将其添加到 describe 'XXXXX' do 之后的行。像魅力一样工作,谢谢@Pyro!
比过滤器更简单的解决方案,+1
我爱你。我欠你一杯啤酒!
这很好。您还可以在“跳过”之后包含一条消息,该消息将显示在输出中。
A
ABMagil

使用 exclusion filters。从该页面:在您的 spec_helper.rb(或 rails_helper.rb)中

RSpec.configure do |c|
  c.filter_run_excluding :broken => true
end

在您的测试中:

describe "group 1", :broken => true do
  it "group 1 example 1" do
  end

  it "group 1 example 2" do
  end
end

describe "group 2" do
  it "group 2 example 1" do
  end
end

当我运行“rspec ./spec/sample_spec.rb --format doc”

然后输出应包含“组 2 示例 1”

并且输出不应包含“组 1 示例 1”

并且输出不应包含“组 1 示例 2”


b
botimer

看看你对此的看法:

describe "something sweet", pending: "Refactor the wazjub for easier frobbing" do
  it "does something well"
  it "rejects invalid input"
end

当我“暂时”禁用某些东西时,我喜欢查看待处理项目的原因。它们用作定期呈现的小评论/待办事项,而不是隐藏在评论或排除的示例/文件中。

it 更改为 pendingxit 既快捷又简单,但我更喜欢哈希构造。它为您提供每次运行的文档,是一个插件(不会更改描述/上下文/它,因此我必须决定稍后再次使用什么),并且如果做出决定或阻止程序被删除,则同样容易删除.

这对于组和单个示例同样适用。


此外,我不确定它是否适用于 describe 但在待定中实际运行测试,如果测试开始通过则失败。 xdescribe(我想就像 xit 一样) - 只是不运行它。
确认这在 rspec 3.6.0 中适用于 pending:skip:。对我来说似乎是最好的解决方案。在 rspec3 中,pending 仍然运行测试,但 skip 不运行(但是您应用了 skip)。
G
GutenYe

另一个。 https://gist.github.com/1300152

使用 xdescribe、xcontext、xit 禁用它。

更新:

从 rspec 2.11 开始,它默认包含 xit。所以新代码将是

# put into spec_helper.rb
module RSpec
  module Core
    module DSL
      def xdescribe(*args, &blk)
        describe *args do
          pending 
        end
      end

      alias xcontext xdescribe
    end
  end
end

用法

# a_spec.rb
xdescribe "padding" do
  it "returns true" do
    1.should == 1
   end
end 

A
Amir Samakar

使用挂起而不是描述。如果你的块是:

context "some other tests" do
  it "should do something destructive" do
    blah
  end
end

您可以通过以下方式跳过整个块:

pending "some other tests" do
  it "should do something destructive" do
    blah
  end
end

M
Matt
describe "GET /blah" do

  before(:each) { pending "Feature to be implemented..." }

  it { expect(page).to have_button("Submit") }
  it { expect(page).to have_content("Blah") }
end

P
PhilT

只是为了解释您的代码发生了什么。将它包含在您拥有的地方,它只会在启动期间加载文件时被评估(并因此运行)。但是,您需要在测试运行时运行它。这就是为什么答案建议将 pending (RSpec 2) 或 skip (RSpec 3) 放入 before 块中。