Reputation: 3665
I am running rspec tests on a catalog
object from within a Ruby app, using 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
(The full code can be found at https://github.com/camptocamp/puppet-spec/blob/master/lib/puppet/indirector/catalog/rest_spec.rb)
In order to pass the object I want to test, I dump it as YAML to a file (currently /tmp/catalog
) and load it as subject in my tests:
describe 'notrun' do
subject { YAML.load_file('/tmp/catalog') }
it { should contain_package('ppet') }
end
Is there a way I could pass the catalog
object as subject to my tests without dumping it to a file?
Upvotes: 1
Views: 284
Reputation: 2752
I am not very clear as to what exactly you are trying to achieve but from my understanding I feel that using a before(:each) hook might be of use to you. You can define variables in this block that are available to all the stories in that scope.
Here is an example:
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
You can find more details at: https://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks#define-before(:each)-block
Upvotes: 1