我使用catalog
在Ruby应用程序中对Rspec::Core::Runner::run
对象运行rspec测试:
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
对象作为我的测试对象而不将其转储到文件中?
答案 0 :(得分: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