How to Mock a Custom Type in Rspec-Puppet

痴心易碎 提交于 2019-12-05 07:08: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.



来源:https://stackoverflow.com/questions/23060142/how-to-mock-a-custom-type-in-rspec-puppet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!