在同一个进程中使用不同的运行器选项多次运行规范

使用 clear_examples 命令在同一个进程中不同运行之间清除所有示例组。它

  require "spec_helper"

  RSpec::Core::Runner.run([... some parameters ...])

  RSpec.clear_examples

  RSpec::Core::Runner.run([... different parameters ...])

背景

给定一个名为“spec/spec_helper.rb”的文件,内容为

RSpec.configure do |config|
  config.filter_run_when_matching :focus => true
  config.filter_run_excluding :slow => true
end

给定一个名为“spec/truth_spec.rb”的文件,内容为

require 'spec_helper'

RSpec.describe "truth" do
  describe true do
    it "is truthy" do
      expect(true).to be_truthy
    end

    it "is not falsy" do
      expect(true).not_to be_falsy
    end
  end

  describe false do
    it "is falsy" do
      expect(false).to be_falsy
    end

    it "is truthy" do
      expect(false).not_to be_truthy
    end
  end
end

在同一个进程中多次运行规范

给定一个名为“scripts/multiple_runs.rb”的文件,内容为

require 'rspec/core'

RSpec::Core::Runner.run(['spec'])
RSpec.clear_examples
RSpec::Core::Runner.run(['spec'])

我运行 ruby scripts/multiple_runs.rb

那么输出应该匹配

4 examples, 0 failures
.*
4 examples, 0 failures

在同一个进程中使用不同的参数多次运行规范

给定一个名为“spec/bar_spec.rb”的文件,内容为

require 'spec_helper'

RSpec.describe 'bar' do
  subject(:bar) { :focused }

  it 'is focused', :focus do
    expect(bar).to be(:focused)
  end
end

给定一个名为“scripts/different_parameters.rb”的文件,内容为

require 'rspec/core'

RSpec::Core::Runner.run(['spec'])
RSpec.clear_examples
RSpec::Core::Runner.run(['spec/truth_spec.rb:4'])
RSpec.clear_examples
RSpec::Core::Runner.run(['spec', '-e', 'fals'])

我运行 ruby scripts/different_parameters.rb

那么输出应该匹配

1 example, 0 failures
.*
2 examples, 0 failures
.*
3 examples, 0 failures