mardi 2 juillet 2019

How to isolate Puppet function mocking in rspec unit tests

I have a Puppet class that uses the result of a custom Puppet function. To make sure I only test the logic in my class, and not the logic in my function when doing unit tests for the class, I want to mock the function.

However, I can't seem to fully isolate my mocked function to a single context. My real testing code is bigger than the following example, but I've boiled it down to this:

class break_tests {

  $result = my_mocked_function('foo', 'bar', 'baz')

  file { 'under_test':
    content => $result,
  }

}

require 'spec_helper'

def mock_mmf(return_value)
  Puppet::Parser::Functions.newfunction(:'my_mocked_function', type: :rvalue) do |_args|
    return return_value
  end
end

# rubocop:disable Metrics/BlockLength
describe 'break_tests' do
  context 'numero uno' do
    before { mock_mmf('foo') }
    it { should contain_file('under_test').with_content('foo') }
  end
  context 'numero duo' do
    before { mock_mmf('bar') }
    it { should contain_file('under_test').with_content('bar') }
  end
end

Failures:

  1) break_tests numero duo should contain File[under_test] with content  supplied string
     Failure/Error: it { should contain_file('under_test').with_content('bar') }
       expected that the catalogue would contain File[under_test] with content set to supplied string
     # ./spec/classes/break_tests_spec.rb:17:in `block (3 levels) in <top (required)>'

I tried splitting it up into two describes and even two separate files, the result is always the same: one context receives the output from a different context.

In my bigger test case, with about 20 tests, it's even more complex, seemingly influenced by whether or not some contexts have facts assigned to them. Ordering of the contexts does not seem to matter.

What am I missing here?

Aucun commentaire:

Enregistrer un commentaire