1

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?

user1571135
  • 199
  • 1
  • 7
  • You should be mocking instead. Take a look at this and see if it helps: https://github.com/Accuity/rspec-puppet-utils#mockresource-experimental-feature – Matthew Schuchard Feb 09 '18 at 13:22
  • Try making it return an array of users, such as `.returns([user1, user])` (you have to define the users variables first) – max pleaner Feb 09 '18 at 16:54
  • @maxpleaner Thanks. I made it work with your suggestion. The solution looks like: `user1=Puppet::Type::type(:user).new( name: 'root', ensure: 'present') Puppet::Type.type(:user).stubs(:instances).returns([user1])` – user1571135 Feb 09 '18 at 21:28
  • @MattSchuchard Thanks for the link, it is something I will look into for other spec test. – user1571135 Feb 09 '18 at 21:31
  • @user1571135 cool, feel free to post your own answer and accept it in 2 days time so that the question won't show up as open. – max pleaner Feb 09 '18 at 21:49
  • @user1571135 I actually misunderstood your question and was looking for something way more complicated than it actually was. The root problem here is that `'User[root]'` is a string and the method `Puppet::Type.instances` returns resource objects in the catalog. I am not sure how you went in that direction, otherwise I would point at some helpful documentation. – Matthew Schuchard Feb 10 '18 at 11:22

1 Answers1

1

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]) 
user1571135
  • 199
  • 1
  • 7