Module: Radius::Spec::Tempfile
- Defined in:
- lib/radius/spec/tempfile.rb
Overview
Temporary file helpers
These helpers are meant to ease the creation of temporary files to either stub the data out or provide a location for data to be saved then verified.
In the case of file stubs, using these helpers allows you to co-locate the file data with the specs. This makes it easy for someone to read the spec and understand the test case; instead of having to find a fixture file and look at its data. This also makes it easy to change the data between specs, allowing them to focus on just what they need.
To make these helpers available require them after the gem:
require 'radius/spec'
require 'radius/spec/tempfile'
Including Helpers in Specs
There are multiple ways you can use these helpers. Which method you choose depends on how much perceived magic/syntactic sugar you want:
- call the helpers directly on the module
- manually include the helper methods in the specs
- use metadata to auto load this feature and include it in the specs
When using the metadata option you do not need to explicitly require the
module. This gem registers metadata with the RSpec configuration when it
loads and RSpec
is defined. When the matching metadata is first used it
will automatically require and include the helpers.
Any of following metadata will include the factory helpers:
:tempfile
:tmpfile
Class Method Summary collapse
-
.using_tempfile(*args, data: nil, **kwargs) {|pathname| ... } ⇒ Object
Convenience wrapper for managaing temporary files.
Class Method Details
.using_tempfile(*args, data: nil, **kwargs) {|pathname| ... } ⇒ Object
A block must be provided
Convenience wrapper for managaing temporary files.
This creates a temporary file and yields its path to the provided block. When the block returns the temporary file is deleted.
Optional Parameters
The block is required. All other parameters are optional. All
parameters except data
are Ruby version dependent and will be
forwarded directly to the stdlib's
Tempfile.create
. The when the data
parameter is provided it's
contents will be written to the temporary file prior to yielding to the
block.
152 153 154 155 156 157 158 159 |
# File 'lib/radius/spec/tempfile.rb', line 152 def using_tempfile(*args, data: nil, **kwargs) args << 'tmpfile' if args.empty? ::Tempfile.create(*args, **kwargs) do |f| f.write(data) f.close yield Pathname(f.path) end end |