Hi Martin,

Using rspec-puppet's pre_condition seems to work for me:

RSpec.shared_context "read_url stub" do
  let (:pre_condition) do
  '
    function profile::read_url(String $arg) >> String {
      return "ssh-rsa AAAAAAAAAzuozgouzvouzvf== [email protected]"
    }
  '
  end
end

RSpec.configure do |c|
  c.include_context "read_url stub"
end

HTH,

Nick

On Fri, Mar 5, 2021 at 7:04 AM Martin Alfke <[email protected]> wrote:

> Hi,
>
> we use a custom function (extlib::read_url) which we want to stub for
> every profile and role class unit test.
>
> We tried using before within Spec.configure:
>
> RSpec.configure do |c|
>   c.before :each do
>     Puppet::Functions.create_function(:'profile::read_url') do
>       dispatch :read_url do
>         param 'Stdlib::HTTPUrl', :url
>         return_type 'String'
>       end
>       def read_url(url)
>         'ssh-rsa AAAAAAAAAzuozgouzvouzvf== [email protected]'
>       end
>     end
>   end
> end
>
> When stubbing the function in spec_helper_local.rb file, we must add the
> dispatch and the def.
>
> But the test still use the original function.
>
> I also tried “overloading” the function in a
> spec/fixtures/override_modules directory.
> Unit tests still take the original function.
>
> Even adding shared_context is not working.
>
> Has anyone an idea, on how to solve this.
> I don’t want to add the function stub (which works flawlessly) into every
> class unit test.
>
> This is a working example where we stub inside the class unit test:
> require 'spec_helper'
>
> describe 'profile::base::user_accounts' do
>   on_supported_os.each do |os, os_facts|
>     context "on #{os}" do
>       let(:facts) { os_facts }
>       Puppet::Functions.create_function(:'profile::read_url') do
>         return 'ssh-rsa AAAAAAAAAzuozgouzvouzvf== [email protected]'
>       end
>
>       it { is_expected.to compile }
>     end
>   end
> end
>
> Best,
> Martin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/422A162D-B1C9-48B1-902C-8E70F1D945A5%40gmail.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CAP15v%2B9WC3ugudPH5mLitVygvdgpj9Zjm_ppJZnPfKTA_uKJBg%40mail.gmail.com.

Reply via email to