Skip to content

Commit

Permalink
add spec_helper/stubloader
Browse files Browse the repository at this point in the history
apparantly a hack to allow rspec mocks to work
  • Loading branch information
rrotter committed Dec 11, 2024
1 parent 35faff4 commit ec5d611
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions spec/spec_helper/stub_loader.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Stub the default dependency loader to do normal resolution except where overridden
def stub_loader!
allow_any_instance_of(Puppet::Pops::Loader::DependencyLoader).to receive(:load).and_call_original
end

# Stub the dependency loader to resolve a named function to a double of some type
#
# @param name [String] The name of the puppet function to stub
# @param dbl [RSpec double] Optional RSpeck double with `call` mocked;
# will be used instead of the block if supplied
# @yield [*args] A block that takes all parameters given to the puppet function;
# required if dbl is not supplied, and ignored if dbl is supplied
def stub_function(name, dbl = nil, &func)
func = dbl || func
stub = ->(_scope, *args, &block) do
func.call(*args, &block)
end
allow_any_instance_of(Puppet::Pops::Loader::DependencyLoader).to receive(:load).with(:function, name).and_return(stub)
end

def stub_with_call(name)
double(name).tap do |dbl|
dbl.define_singleton_method(:allow_call) do
allow(dbl).to receive(:call)
end
stub_function(name, dbl)
end
end

def stub(name)
dbl = double(name)

yield dbl
stub_function(name, dbl)
end

def allow_call(dbl)
allow(dbl).to receive(:call)
end
1 change: 1 addition & 0 deletions spec/spec_helper_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require 'spec_helper/deep_merge'
require 'spec_helper/mock_function'
include RSpecPuppetUtils
require 'spec_helper/stub_loader'

0 comments on commit ec5d611

Please sign in to comment.