Search code examples
rubyrspecpuppetrspec-puppet

How to stub Puppet::Type('user).instances.select


In a fact Im calling

Puppet::Type('user').instances.select do |user|
#do something with user
end

How can I stub that in a spec test? I have something like:

Puppet::Type.type(:user).stubs(:instances).returns(
    'User[root]','User[bin]'])

but that stubs it with an array and not a User object. How can I stub it correctly?


Solution

  • The solution was to ensure that the return value was a user object. I did that by:

    user1=Puppet::Type::type(:user).new( name: 'root', ensure: 'present') 
    Puppet::Type.type(:user).stubs(:instances).returns([user1])