From 9350f5e3d3b6caaa2b8142e72289dbd65948e596 Mon Sep 17 00:00:00 2001 From: Rob Nelson Date: Mon, 24 Apr 2023 15:25:32 -0400 Subject: [PATCH 1/2] Add information on mocking an external function When not testing the function itself, you still need to mock it so it's available to the calling class. --- docs/documentation/functions/index.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/documentation/functions/index.md b/docs/documentation/functions/index.md index 5226cbaf8..a62e091ca 100644 --- a/docs/documentation/functions/index.md +++ b/docs/documentation/functions/index.md @@ -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| + { + 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. From 311c55cdc195948b1564be393436096a1cd6d676 Mon Sep 17 00:00:00 2001 From: Rob Nelson Date: Tue, 25 Apr 2023 13:32:14 +0000 Subject: [PATCH 2/2] Denote possible spec helper file paths for PDK and non-PDK users --- docs/documentation/functions/index.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/documentation/functions/index.md b/docs/documentation/functions/index.md index a62e091ca..4239776eb 100644 --- a/docs/documentation/functions/index.md +++ b/docs/documentation/functions/index.md @@ -102,8 +102,9 @@ end 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: +Modify *spec/spec_helper.rb* (or *spec/spec_helper__local.rb* if you use the PDK) and +use `Puppet::Parser::Functions.newfunction()` to add the function inside of an +`RSpec.configure` block. For example: {% highlight ruby %} RSpec.configure do | c|