2

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
Community
  • 1
  • 1
030
  • 10,842
  • 12
  • 78
  • 123

1 Answers1

2

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.

Felix Frank
  • 8,125
  • 1
  • 23
  • 30
  • I have tried to implement the examples, but the error `Invalid resource type custom_multiple_files` persists – 030 Apr 17 '14 at 19:34
  • Ah, I see. This is interesting additional information. I shall post another answer. – Felix Frank Apr 22 '14 at 00:57
  • `let(:type) { Puppet::Type.type(:custom_multiple_files) }` results in: 2) vim::ubuntu::config Failure/Error: let(:type) { Puppet::Type.type(:custom_multiple_files) } NoMethodError: undefined method `let' for # # ./spec/classes/ubuntu_config_spec.rb:12 – 030 Apr 22 '14 at 08:51
  • I've since tinkered with `rspec-puppet` a bit and stubbing a type for a test does not seem to be possible in a way as simple as I had in mind. The reason is likely the mucking that `rspec-puppet` is doing with the actual `rspec` framework. Bottom line is, even if it's possible to stub the type away, I suspect that the usual route of symlinking the respective module(s) into `spec/fixtures` is too much easier to pass up ;-) – Felix Frank Apr 22 '14 at 22:30