From 4184c54118e289c40d33dbc04cf2d82ad05c0c0d Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Thu, 29 Dec 2011 15:37:24 -0800 Subject: [PATCH] Add with method to generic matcher This commit adds a method 'with' that can be used to test mulitple parameters/values with a single method call Example: it do should contain_service('keystone').with( 'ensure' => 'running', 'enable' => 'true', 'hasstatus' => 'true', 'hasrestart' => 'true' ) end It was created as a more condensed alternative to chaining methods per parameter to validate. --- README.md | 11 +++++++++++ lib/rspec-puppet/matchers/create_generic.rb | 5 +++++ spec/classes/sysctl_common_spec.rb | 13 +++++++++++++ 3 files changed, 29 insertions(+) diff --git a/README.md b/README.md index f18c8bb..47da48e 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,17 @@ the generic `with_` chains. it { should contain_package('mysql-server').with_ensure('present') } ``` +You can use the `with` method to verify the value of multiple parameters. + +```ruby +it do should contain_service('keystone').with( + 'ensure' => 'running', + 'enable' => 'true', + 'hasstatus' => 'true', + 'hasrestart' => 'true' +) end +``` + You can also test that specific parameters have been left undefined with the generic `without_` chains. diff --git a/lib/rspec-puppet/matchers/create_generic.rb b/lib/rspec-puppet/matchers/create_generic.rb index 286c279..ecca2fc 100644 --- a/lib/rspec-puppet/matchers/create_generic.rb +++ b/lib/rspec-puppet/matchers/create_generic.rb @@ -9,6 +9,11 @@ module RSpec::Puppet @title = args[0] end + def with(*args, &block) + params = args.shift + @expected_params = (@expected_params || []) | params.to_a + self + end def method_missing(method, *args, &block) if method.to_s =~ /^with_/ param = method.to_s.gsub(/^with_/, '') diff --git a/spec/classes/sysctl_common_spec.rb b/spec/classes/sysctl_common_spec.rb index 7938510..390b0d1 100644 --- a/spec/classes/sysctl_common_spec.rb +++ b/spec/classes/sysctl_common_spec.rb @@ -4,6 +4,19 @@ describe 'sysctl::common' do it { should contain_exec('sysctl/reload') \ .with_command('/sbin/sysctl -p /etc/sysctl.conf').with_returns([0, 2]) } it { should_not create_augeas('foo') } + describe 'when using with to specify a hash of parameters' do + it 'should fail if the parameter is not contained in the resource' do + expect do + subject.should contain_exec('sysctl/reload').with('foo' => 'bar') + end.should raise_error(RSpec::Expectations::ExpectationNotMetError) + end + it 'should pass if the parameters are contained in the resource' do + subject.should contain_exec('sysctl/reload').with( + 'refreshonly' => 'true', + 'returns' => [0, 2] + ) + end + end end describe 'sysctl::common' do