Skip to content

Using the documented mocking of functions throws errors #12

@jeffbyrnes

Description

@jeffbyrnes

With a unit test like this:

let!(:vault_lookup) do
  MockFunction.new('vault_lookup') do |f|
    f.stubbed.returns('pwhash' => 'TEST_PASSWORD')
  end
end

it { is_expected.to compile }

Using PDK to call Rspec, like so:

♠ pdk test unit
pdk (INFO): Using Ruby 2.7.8
pdk (INFO): Using Puppet 7.34.0
[✔] Preparing to run the unit tests.
/opt/puppetlabs/pdk/private/ruby/2.7.8/bin/ruby -I/Users/rbyrnes/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.13.3/lib:/Users/rbyrnes/.pdk/cache/ruby/2.7.0/gems/rspec-support-3.13.2/lib /Users/rbyrnes/.pdk/cache/ruby/2.7.0/gems/rspec-core-3.13.3/exe/rspec --pattern spec/\{aliases,classes,defines,functions,hosts,integration,plans,tasks,type_aliases,types,unit\}/\*\*/\*_spec.rb --format progress
# snip

Results in errors like this one:

  18) corebasic on oraclelinux-8-x86_64
      Failure/Error: f.stubbed.returns('pwhash' => 'TEST_PASSWORD')

      NoMethodError:
        undefined method `stubs' for #<RSpecPuppetUtils::MockFunction:0x000000013fe9d500>
        Did you mean?  stub
      # ./spec/classes/corebasic_spec.rb:15:in `block (5 levels) in <top (required)>'
      # ./spec/classes/corebasic_spec.rb:14:in `new'
      # ./spec/classes/corebasic_spec.rb:14:in `block (4 levels) in <top (required)>'

If we adjust the mock per the docs:

let!(:vault_lookup) do
  MockFunction.new('vault_lookup') do |f|
    f.stubbed('ansible').returns('pwhash' => 'TEST_PASSWORD')
  end
end

it { is_expected.to compile }

We get this error:

  18) corebasic on oraclelinux-8-x86_64
      Failure/Error: f.stubbed('ansible').returns('pwhash' => 'TEST_PASSWORD')

      ArgumentError:
        wrong number of arguments (given 1, expected 0)
      # ./spec/classes/corebasic_spec.rb:15:in `block (5 levels) in <top (required)>'
      # ./spec/classes/corebasic_spec.rb:14:in `new'
      # ./spec/classes/corebasic_spec.rb:14:in `block (4 levels) in <top (required)>'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions