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.
This commit is contained in:
parent
48407ca8c5
commit
4184c54118
11
README.md
11
README.md
|
@ -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.
|
||||
|
||||
|
|
|
@ -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_/, '')
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue