BeastMode
BeastMode

Reputation: 3

rspec-puppet tests fail undefined method

I'm trying to write my first rspec test for a simple puppet class. Here's the class, rspec test and results. I'm new to rspec and would like to know what I'm doing wrong here. I follow the directions here http://rspec-puppet.com/setup/ to configure rspec-puppet for these tests. Thanks.

Class example for cron module init.pp

class cron {

        service { 'crond' :
                ensure => running,
                enable => true
    }
}

Rspec Test

require '/etc/puppetlabs/puppet/modules/cron/spec/spec_helper'

describe 'cron', :type => :module do

      it { should contain_class('cron') }
      it do should contain_service('crond').with(
    'ensure' => 'running',
    'enable' => 'true'
      ) end
end

Results

FF

Failures:

  1) cron
     Failure/Error: it { should contain_class('cron') }
     NoMethodError:
       undefined method `contain_class' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000001c66d70>
     # ./cron_spec.rb:5:in `block (2 levels) in <top (required)>'

  2) cron
     Failure/Error: it do should contain_service('crond').with(
     NoMethodError:
       undefined method `contain_service' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000001c867b0>
     # ./cron_spec.rb:6:in `block (2 levels) in <top (required)>'

Finished in 0.00237 seconds
2 examples, 2 failures

Failed examples:

rspec ./cron_spec.rb:5 # cron
rspec ./cron_spec.rb:6 # cron

Upvotes: 0

Views: 1452

Answers (1)

Felix Frank
Felix Frank

Reputation: 8223

Where did you pick up the

describe 'cron', :type => :module

syntax? That may be obsolete.

With current versions of rspec-puppet, you describe

  • classes
  • defined types
  • functions
  • hosts

You basically just want to put your spec right into spec/classes/cron_spec.rb, that should do half your work for you, e.g.

# spec/classes/cron.rb
require "#{File.join(File.dirname(__FILE__),'..','spec_helper.rb')}"

describe 'cron' do
  it { should contain_service('crond').with('ensure' => 'running') }
  it { should contain_service('crond').with('enable' => 'true') }
end

It is good practice to have distinct tests for each attribute value, so that possible future regressions can be identified more accurately.

Do see the README.

For a nice example of a well structured module test-suite see example42's modules.

Upvotes: 2

Related Questions