-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add information on mocking an external function #51
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the legacy function API. The current standard should be used everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
def find_function(function_name = self.class.top_level_description) | |
with_vardir do | |
env = adapter.current_environment | |
if Puppet.version.to_f >= 4.0 | |
context_overrides = compiler.context_overrides | |
func = nil | |
loaders = Puppet.lookup(:loaders) | |
Puppet.override(context_overrides, 'rspec-test scope') do | |
func = V4FunctionWrapper.new(function_name, | |
loaders.private_environment_loader.load(:function, function_name), context_overrides) | |
@scope = context_overrides[:global_scope] | |
end | |
return func if func.func | |
end | |
if Puppet::Parser::Functions.function(function_name) | |
V3FunctionWrapper.new(function_name, scope.method("function_#{function_name}".intern)) | |
end | |
end | |
end |
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.