Skip to content

Commit

Permalink
Add with method to generic matcher
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dan Bode committed Dec 29, 2011
1 parent 48407ca commit 4184c54
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -88,6 +88,17 @@ the generic `with_<parameter>` 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_<parameter>` chains.

Expand Down
5 changes: 5 additions & 0 deletions lib/rspec-puppet/matchers/create_generic.rb
Expand Up @@ -9,6 +9,11 @@ def initialize(*args, &block)
@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_/, '')
Expand Down
13 changes: 13 additions & 0 deletions spec/classes/sysctl_common_spec.rb
Expand Up @@ -4,6 +4,19 @@
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
Expand Down

0 comments on commit 4184c54

Please sign in to comment.