Ravn::SpecHelpers::
BaseHelpers
module
A collection of test helper functions and classes.
- TEST_DATA_DIR
The directory to look in for fixture data
Clean any files generated during a spec run.
def self::cleanup_tmpfiles
Pathname( Dir::Tmpname.tmpdir ).children.
select{|f| f.basename.to_s.start_with?(/ravn.*\-test\-/) }.
each( &:rmtree )
end
Inclusion callback – install some hooks
def self::included( context )
context.before( :all ) do
Loggability.logger.warn "Setting up a temporary device ID file."
Ravn::SpecHelpers::BaseHelpers.setup_device_id
end
context.after( :each ) do
Ravn::SpecHelpers::RecordingActor.instance = nil
end
context.after( :all ) do
Ravn::SpecHelpers::BaseHelpers.cleanup_tmpfiles
end
end
have_a_line_matching( regexp )
RSpec matcher – set up the expectation that the lefthand side is Enumerable, and that at least one of the objects yielded while iterating matches regexp
when converted to a String.
def have_a_line_matching( regexp )
return HaveALineMatching.new( regexp )
end
load_fixture_data( name )
Load data from the spec/data directory with the specified name
, deserializing it if it’s YAML or JSON, and returning it.
def load_fixture_data( name )
name = name.to_s
path = TEST_DATA_DIR + name
path = TEST_DATA_DIR + "#{name}.json" unless path.exist? || !File.extname(name).empty?
path = TEST_DATA_DIR + "#{name}.yaml" unless path.exist? || !File.extname(name).empty?
path = TEST_DATA_DIR + "#{name}.yml" unless path.exist? || !File.extname(name).empty?
path = TEST_DATA_DIR + "#{name}.txt" unless path.exist? || !File.extname(name).empty?
rawdata = path.read( encoding: 'utf-8' )
return case path.extname
when '.json'
Yajl::Parser.parse( rawdata )
when '.yml', '.yaml'
Psych.safe_load( rawdata, symbolize_names: true )
else
rawdata
end
end
Set the pathname of the device ID file to a temporary file.
def setup_device_id
Ravn.device_id_file = tmpfile_pathname( 'device-id' )
Loggability.logger.warn "Setting device ID file to %s" % [ Ravn.device_id_file ]
Ravn.device_id
end
spawn_recording_actor( name_or_opts )
Spawn a RecordingActor
and return its reference.
def spawn_recording_actor( name_or_opts )
return Ravn::SpecHelpers::RecordingActor.spawn!( name_or_opts )
end
tmpfile_pathname( filetype='spec' )
Return a Pathname pointing to a temporary file.
def tmpfile_pathname( filetype='spec' )
Pathname(Dir::Tmpname.create(['ravn-', '-test-' + filetype]) {})
end