禁用 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

但就像我说的,它只是继续在未决呼叫下运行测试。

搜索引导我到这个 邮件列表线程其中的创造者(?)Rspec 的数据显示在 Rspec 2中是可能的,我正在运行。我想它确实起作用了,但是它没有达到禁用以下所有测试的预期效果,这就是我在看到 pending调用时所想到的。

还有别的选择吗,还是我做错了?

51278 次浏览

Use exclusion filters. From that page: In your spec_helper.rb (or rails_helper.rb)

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

In your test:

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

When I run "rspec ./spec/sample_spec.rb --format doc"

Then the output should contain "group 2 example 1"

And the output should not contain "group 1 example 1"

And the output should not contain "group 1 example 2"

another one. https://gist.github.com/1300152

use xdescribe, xcontext, xit to disable it.

Update:

Since rspec 2.11, it includes xit by default. so the new code will be

# 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

Usage

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

To disable a tree of specs using RSpec 3 you can:

before { skip }
# or
xdescribe
# or
xcontext

You can add a message with skip that will show up in the output:

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

with RSpec 2:

before { pending }

Use pending instead of describe. If your block is:

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

You can skip the whole block by:

pending "some other tests" do
it "should do something destructive" do
blah
end
end
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

See what you think of this:

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

I like to see reasons with my pending items when I'm disabling something for "a while". They serve as little comments/TODOs that are presented regularly rather than buried in a comment or an excluded example/file.

Changing it to pending or xit is quick and easy, but I prefer the hash construction. It gives you every-run documentation, is a drop-in (doesn't change describe/context/it so I have to decide what to use again later), and is just as easily removed if the decision is made or blocker is removed.

This works the same for groups and individual examples.

Just to explain what's happening with your code. Including it where you have, it just gets evaluated (and hence run) when the file is loaded during startup. However you need it to be run when the tests run. That's why answers have suggested putting pending (RSpec 2) or skip (RSpec 3) into a before block.