Skip to content

Hiera Data Validator

TomPoulton edited this page Mar 24, 2014 · 1 revision

The recommended way of using the validator is like so:

describe ... do

  validator = HieraData::YamlValidator.new('path/to/yaml')
  validator.load_data

  it ... do
    validator.validate(...) { |v| ... }
  end

end

There are a couple of things to note here, one is that we don't wrap the validator with a let or subject, and the other is that if there are errors when loading the files (syntax errors or empty files), these don't get raised until the validate method gets called.

So far this is the cleanest way of achieving all of the following requirements:

  • Only load data once - This is for performance, I've heard stories of people having a large number of data files so we really don't want to have to go back to the file-system for every test.
  • Load errors should be handled gracefully - We shouldn't let errors blow up in a describe or context block as it's not very clean, ideally any errors are caught within tests (it blocks)
  • Tests can run independently - We could call load_data within a test and catch syntax errors there, but then all other test rely on that load test being run first!
  • All errors are easy to find - If there are data load errors we want the tests to tell us where they are so we can fix them
  • Avoid unnecessary code as much as possible - Specs should be about the tests, not about the testing framework

If there's a better way of doing this let me know!

Clone this wiki locally