Automatic class parameters and rspec testing

asked 2018-10-17 22:30:58 -0600

glisignoli gravatar image

I have a module with a class and a subclass, the first class:

class foo {
  class { 'bar': }
}

And the sub class

class foo::bar (
  $baz = "yes"
) {
  if $baz == "yes" {
    fail('baz is yes')
  }
}

Now I have a rspec test

require 'spec_helper'
describe 'foo' do
  context 'with default values' do
    it { should compile.and_raise_error(/baz is yes/)
  end
end

Which will test for the failure but how do I write a second test for the foo module that tests for different parameters being set on foo::bar when automatic class parameters are used?

I thought I could do something like:

require 'spec_helper'
describe 'foo' do
  context 'with default values' do
    it { should compile.and_raise_error(/baz is yes/)
  end

  context 'with foo::bar::baz' do
    let(:params) do
      {
        'foo::bar::baz' => 'no'
      }
    end
  it { is_expected.to compile }
  it { is_expected.to compile.with_all_deps }
  end
end

But that's trying to pass the value 'no' to a parameter that doesn't exist on the class foo. In my module it is expected that the user can override the subclass if they need to, but I can't see any way to test for this.

edit retag flag offensive close merge delete