1

我正在catalog从 Ruby 应用程序中对对象运行 rspec 测试,使用Rspec::Core::Runner::run

File.open('/tmp/catalog', 'w') do |out|
  YAML.dump(catalog, out)
end

...

unless RSpec::Core::Runner::run(spec_dirs, $stderr, out) == 0
  raise Puppet::Error, "Unit tests failed:\n#{out.string}"
end

(完整代码可以在https://github.com/camptocamp/puppet-spec/blob/master/lib/puppet/indirector/catalog/rest_spec.rb找到)

为了传递我想要测试的对象,我将它作为 YAML 转储到一个文件(当前/tmp/catalog)并在我的测试中将它作为主题加载:

describe 'notrun' do
  subject { YAML.load_file('/tmp/catalog') }
  it { should contain_package('ppet') }
end

有没有一种方法可以将catalog对象作为测试对象传递而不将其转储到文件中?

4

1 回答 1

1

我不太清楚您到底要达到什么目标,但根据我的理解,我觉得使用 before(:each) 钩子可能对您有用。您可以在此块中定义可用于该范围内所有故事的变量。

这是一个例子:

require "rspec/expectations"

class Thing
  def widgets
    @widgets ||= []
  end
end

describe Thing do
  before(:each) do
    @thing = Thing.new
  end

  describe "initialized in before(:each)" do
    it "has 0 widgets" do
      # @thing is available here
      @thing.should have(0).widgets
    end

    it "can get accept new widgets" do
      @thing.widgets << Object.new
    end

    it "does not share state across examples" do
      @thing.should have(0).widgets
    end
  end
end

您可以在以下位置找到更多详细信息: https ://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks#define-before(:each)-block

于 2013-04-03T12:43:13.637 回答