-
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -97,3 +97,28 @@ describe 'ensure_resource' do | |||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||
{% endhighlight %} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
## Mocking a function for use in all tests | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
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()` | ||||||||||||||||||||||||||||||||||||||||||||||
to add the function inside of an `RSpec.configure` block. For example: | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
{% highlight ruby %} | ||||||||||||||||||||||||||||||||||||||||||||||
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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I'm looking at the function testing code and 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. |
||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
domain: 'test', | ||||||||||||||||||||||||||||||||||||||||||||||
username: 'test', | ||||||||||||||||||||||||||||||||||||||||||||||
password: 'test' | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||
{% endhighlight %} | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
You may add multiple functions and other configuration elements inside the `c.before :each do` | ||||||||||||||||||||||||||||||||||||||||||||||
block and all will be available inside every test. |
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 mentionspec_helper_local.rb
anywhere. That's really a PDK-thing and I'd avoid it here.