How to Mock a Custom Type in Rspec-Puppet

2020-06-27 09:02发布

问题:

I am able to test custom types using rspec-puppet due to implementing the answer of this question.

However, I would like to avoid to create a symlink to the custom folder in every puppet-module by mocking cystom types.

The question is how to mock Custom Puppet Types in Rspec-Puppet.

I have found an example regarding the mocking of a Custom Puppet Function but I am looking for an example to mock Puppet Custom Types.

Puppet Code

class vim::ubuntu::config {
  custom_multiple_files { 'line_numbers':
    ensure     => 'present',
    parent_dir => '/home',
    file_name  => '.vimrc',
    line       => 'set number';
  }
}

Rspec-puppet code

require 'spec_helper'

describe "vim::ubuntu::config" do
  ?
end

回答1:

A good place to go looking for examples on mocking is the collection of Puppet's own unit tests.

I'm not sure if there are an specialties that needs to be considered, but within Puppet's spec test, the mocking works like this:

let(:type) { Puppet::Type.type(:custom_file_line) }
it "should do whatever"
  type.stubs(:retrieve).returns <value>
  # perhaps also needed
  Puppet::Type.stubs(:type).with(:custom_file_line).returns(type)

From what I understand, this is moccha style mocking. In plain rspec, mocking/stubbing is a bit more involved, which may be necessary with rspec-puppet.