Skip to content

Commit

Permalink
Use instance doubles
Browse files Browse the repository at this point in the history
This verifies in the stubbed methods match the function signatures.
  • Loading branch information
ekohl committed Apr 28, 2023
1 parent cfa5cd4 commit 2cd755d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions spec/beaker_puppet_helpers/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def logger
describe BeakerPuppetHelpers::DSL do
subject(:dsl) { ClassMixedWithDSLHelpers.new }

let(:master) { double('Beaker::Host') }
let(:agent) { double('Beaker::Host') }
let(:master) { instance_double(Beaker::Host) }
let(:agent) { instance_double(Beaker::Host) }
let(:hosts) { [master, agent] }

describe '#apply_manifest_on' do
Expand Down Expand Up @@ -179,7 +179,7 @@ def logger

describe '#fact_on' do
it 'retrieves a fact on a single host' do
result = double('Beaker::Result', stdout: '{"osfamily": "family"}')
result = instance_double(Beaker::Result, stdout: '{"osfamily": "family"}')
expect(dsl).to receive(:on).and_return(result)

expect(dsl.fact_on('host', 'osfamily')).to eq('family')
Expand All @@ -188,21 +188,21 @@ def logger
it 'converts each element to a structured fact when it receives an array of results from #on' do
times = hosts.length

result = double('Beaker::Result', stdout: '{"os": {"name":"name", "family": "family"}}')
result = instance_double(Beaker::Result, stdout: '{"os": {"name":"name", "family": "family"}}')
allow(dsl).to receive(:on).and_return([result] * times)

expect(dsl.fact_on(hosts, 'os')).to eq([{ 'name' => 'name', 'family' => 'family' }] * times)
end

it 'returns a single result for single host' do
result = double('Beaker::Result', stdout: '{"osfamily": "family"}')
result = instance_double(Beaker::Result, stdout: '{"osfamily": "family"}')
allow(dsl).to receive(:on).and_return(result)

expect(dsl.fact_on('host', 'osfamily')).to eq('family')
end

it 'preserves data types' do
result = double('Beaker::Result', stdout: '{"identity": { "uid": 0, "user": "root", "privileged": true }}')
result = instance_double(Beaker::Result, stdout: '{"identity": { "uid": 0, "user": "root", "privileged": true }}')
allow(dsl).to receive(:on).and_return(result)

structured_fact = dsl.fact_on('host', 'identity')
Expand Down
2 changes: 1 addition & 1 deletion spec/beaker_puppet_helpers/install_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe BeakerPuppetHelpers::InstallUtils do
describe '.install_puppet_release_repo_on' do
let(:host) { double('Beaker::Host') }
let(:host) { instance_double(Beaker::Host) }
let(:packaging_platform) { Beaker::Platform.new(platform) }

before { allow(host).to receive(:[]).with('packaging_platform').and_return(packaging_platform) }
Expand Down
4 changes: 2 additions & 2 deletions spec/beaker_puppet_helpers/module_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def logger
describe BeakerPuppetHelpers::ModuleUtils do
subject(:dsl) { ClassMixedWithDSLInstallUtils.new }

let(:host) { double('Beaker::Host') }
let(:host) { instance_double(Beaker::Host) }

describe '#install_puppet_module_via_pmt_on' do
let(:default_module_install_opts) { nil }
Expand Down Expand Up @@ -82,7 +82,7 @@ def logger
end

describe '#install_local_module_on' do
let(:builder) { double('Puppet::Modulebuilder::Builder') }
let(:builder) { instance_double(Puppet::Modulebuilder::Builder) }

it 'builds and copies the module' do
expect(File).to receive(:realpath).with('.').and_return('/path/to/module')
Expand Down

0 comments on commit 2cd755d

Please sign in to comment.