Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be less strict when using #hash_including with defaults #674

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,35 @@
]
end
end

# Similar to RSpec’s built-in #hash_including, but does not require the keys of
# `expected` to be present in `actual` -- what matters is that for all keys in
# `expected`, the value in `actual` and `expected` is the same.
#
# hash = Hash.new { |hash, key| 9000 }
# moo(hash)
#
# This passes:
#
# expect(something)
# .to receive(:moo)
# .with(hash_with_defaults_including(stuff: 9000))
#
# This does not pass:
#
# expect(something)
# .to receive(:moo)
# .with(hash_including(stuff: 9000))
RSpec::Matchers.define :hash_with_defaults_including do |expected|
include RSpec::Matchers::Composable

match do |actual|
expected.keys.all? do |key|
values_match?(expected[key], actual[key])
end
end

description do
"hash_with_defaults_including(#{expected.inspect})"
end
end
2 changes: 1 addition & 1 deletion spec/unit/pdk/cli/build_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
include_context 'exits cleanly'

it 'invokes the builder with the default target directory' do
expect(PDK::Module::Build).to receive(:new).with(hash_including(:'target-dir' => File.join(Dir.pwd, 'pkg'))).and_return(mock_builder)
expect(PDK::Module::Build).to receive(:new).with(hash_with_defaults_including(:'target-dir' => File.join(Dir.pwd, 'pkg'))).and_return(mock_builder)
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/unit/pdk/cli/test/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

context 'when passed --clean-fixtures' do
it 'invokes the command with :clean-fixtures => true' do
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_including(:puppet => puppet_version, :tests => anything, :'clean-fixtures' => true)).once.and_return(0)
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_with_defaults_including(:puppet => puppet_version, :tests => anything, :'clean-fixtures' => true)).once.and_return(0)
expect {
test_unit_cmd.run_this(['--clean-fixtures'])
}.to exit_zero
Expand All @@ -110,7 +110,7 @@

context 'when not passed --clean-fixtures' do
it 'invokes the command without :clean-fixtures' do
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_including(puppet: puppet_version, tests: anything)).once.and_return(0)
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_with_defaults_including(puppet: puppet_version, tests: anything)).once.and_return(0)
expect {
test_unit_cmd.run_this([])
}.to exit_zero
Expand All @@ -119,7 +119,7 @@

context 'when tests pass' do
before(:each) do
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_including(:tests)).once.and_return(0)
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_with_defaults_including(tests: anything)).once.and_return(0)
end

it do
Expand Down Expand Up @@ -151,7 +151,7 @@

context 'when tests fail' do
before(:each) do
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_including(:tests)).once.and_return(1)
expect(PDK::Test::Unit).to receive(:invoke).with(reporter, hash_with_defaults_including(tests: anything)).once.and_return(1)
end

it do
Expand Down