Add information on mocking an external function#51
Add information on mocking an external function#51rnelson0 wants to merge 2 commits intopuppetlabs:mainfrom
Conversation
When not testing the function itself, you still need to mock it so it's available to the calling class.
|
|
||
| You may not need to test a function itself, but a calling class may require the function | ||
| to return a value. You can mock the function once and make it available to all test. | ||
| Modify *spec/spec_helper_local.rb* and use `Puppet::Parser::Functions.newfunction()` |
There was a problem hiding this comment.
rspec-puppet really doesn't mention spec_helper_local.rb anywhere. That's really a PDK-thing and I'd avoid it here.
| RSpec.configure do | c| | ||
| c.before :each do | ||
| # The vault_lookup function takes a single argument and returns a hash with three keys | ||
| Puppet::Parser::Functions.newfunction(:vault_lookup, type: :rvalue) do |_args| |
There was a problem hiding this comment.
This is the legacy function API. The current standard should be used everywhere.
There was a problem hiding this comment.
There's some discussion of that in slack and how it would apply to tests, there's not a lot of clarity there and the "obvious" code doesn't seem to work. Very happy to move to that if we can determine the correct syntax to use.
There was a problem hiding this comment.
I'm looking at the function testing code and Puppet.override looks promising. It is a private API of Puppet, but looks promising.
rspec-puppet/lib/rspec-puppet/example/function_example_group.rb
Lines 87 to 108 in f4c5c71
Another problem you may have with the modern API is that it prefers namespaced functions, or there's a difference in priority.
When not testing the function itself, you still need to mock it so it's available to the calling class.