是否有一个用于Rails测试的setup_class/teardown_class?

Joe*_*ean 10 ruby ruby-on-rails activesupport

我需要为类或系统范围的一些Rails测试设置一个设置和拆卸方法,但我只找到了一种方法来定义一个适用于每个测试级别的常规设置/拆卸.

例如:

class ActiveSupport::TestCase
  setup do
    puts "Setting up"
  end

  teardown do
    puts "tearing down"
  end
end
Run Code Online (Sandbox Code Playgroud)

将执行每个测试用例的输出,但我想要像:

class ActiveSupport::TestCase
  setup_fixture do
    puts "Setting up"
  end

  teardown_fixture do
    puts "tearing down"
  end
end
Run Code Online (Sandbox Code Playgroud)

这将执行安装_夹具之前所有的测试方法,然后执行拆卸_夹具所有的测试方法.

有这样的机制吗?如果没有,是否有一种简单的方法来修补这种机制?

Jam*_*sen 4

有几种流行的测试框架构建Test::Unit并提供这种行为:

规格

describe "A Widget" do
  before(:all) do
    # stuff that gets run once at startup
  end
  before(:each) do
    # stuff that gets run before each test
  end
  after(:each) do
    # stuff that gets run after each test
  end
  after(:all) do
    # stuff that gets run once at teardown
  end
end
Run Code Online (Sandbox Code Playgroud)

测试/规格

context "A Widget" do
  # same syntax as RSpec for before(:all), before(:each), &c.
end
Run Code Online (Sandbox Code Playgroud)