问题描述:

I am getting the following error in travis ci when committing a project:

Failures:

1) salt on unsupported distributions we fail

Failure/Error: expect { subject }.to raise_error(/Unsupported platform: Unsupported/)

expected Exception with message matching /Unsupported platform: Unsupported/ but nothing was raised

# ./spec/classes/salt_spec.rb:9:in `block (3 levels) in <top (required)>'

Finished in 6.5 seconds

47 examples, 1 failure

Failed examples:

rspec ./spec/classes/salt_spec.rb:8 # salt on unsupported distributions we fail

/home/travis/.rvm/rubies/ruby-2.0.0-p598/bin/ruby -S rspec spec/classes/salt_spec.rb --color failed

And here is the salt_spec.rb:

require 'spec_helper'

describe 'salt' do

context 'on unsupported distributions' do

let(:facts) {{ :osfamily => 'Unsupported' }}

it 'we fail' do

expect { subject }.to raise_error(/Unsupported platform: Unsupported/)

end

end

['Debian', 'RedHat', 'SUSE', ].each do |distro|

context "on #{distro}" do

let(:facts) {{

:osfamily => distro,

}}

it { should contain_class('salt::master::install') }

it { should contain_class('salt::master::config') }

it { should contain_class('salt::master::service') }

it { should contain_class('salt::minion::install') }

it { should contain_class('salt::minion::config') }

it { should contain_class('salt::minion::service') }

##

## salt-master config file

##

describe 'config file with default params' do

it { should contain_file('/etc/salt/master')}

end

##

## salt-minion config file

##

describe 'config file with default params' do

it { should contain_file('/etc/salt/minion')}

end

##

## salt-master service

##

describe 'service with default params' do

it { should contain_service('salt-master').with(

'ensure' => 'running',

'enable' => 'true',

'hasstatus' => 'true',

'hasrestart' => 'true'

)}

end

##

## salt-minion service

##

describe 'service with default params' do

it { should contain_service('salt-minion').with(

'ensure' => 'running',

'enable' => 'true',

'hasstatus' => 'true',

'hasrestart' => 'true'

)}

end

##

## salt::master::install

##

it 'installs the salt-master package' do

should contain_package('salt-master').with(

'ensure' => 'present',

'name' => 'salt-master'

)

end

##

## salt::minion::install

##

it 'installs the salt-minion package' do

should contain_package('salt-minion').with(

'ensure' => 'present',

'name' => 'salt-minion'

)

end

end

end

['Archlinux', ].each do |distro|

context "on #{distro}" do

let(:facts) {{

:osfamily => distro,

}}

it { should contain_class('salt::master::install') }

it { should contain_class('salt::master::config') }

it { should contain_class('salt::master::service') }

it { should contain_class('salt::minion::install') }

it { should contain_class('salt::minion::config') }

it { should contain_class('salt::minion::service') }

##

## salt-master config file

##

describe 'config file with default params' do

it { should contain_file('/etc/salt/master')}

end

##

## salt-minion config file

##

describe 'config file with default params' do

it { should contain_file('/etc/salt/minion')}

end

##

## salt-master service

##

describe 'service with default params' do

it { should contain_service('salt-master').with(

'ensure' => 'running',

'enable' => 'true',

'hasstatus' => 'true',

'hasrestart' => 'true'

)}

end

##

## salt-minion service

##

describe 'service with default params' do

it { should contain_service('salt-minion').with(

'ensure' => 'running',

'enable' => 'true',

'hasstatus' => 'true',

'hasrestart' => 'true'

)}

end

end

end

end

If I understand the block (3 levels) in <top (required)> correctly then the indentation is wrong somewhere between line 5 and 11, however it appears right to me. Any and all help with this would be greatly appreciated as this is my first time dealing with rspec and I am trying to learn.

EDIT

I have fixed this myself. I changed my code from this:

 context 'on unsupported distributions' do

let(:facts) {{ :osfamily => 'Unsupported' }}

it 'we fail' do

expect { subject }.to raise_error(/Unsupported platform: Unsupported/)

end

end

To this:

 context 'on unsupported distributions' do

let(:facts) {{ :osfamily => 'Unsupported' }}

it 'we fail' do

should compile.and_raise_error(/Unsupported platform: Unsupported/)

end

end

And it appears to be passing now. I guess my question is this: Is this correct or am I incorrectly passing the test?

网友答案:

Resolved this by changing the following:

context 'on unsupported distributions' do let(:facts) {{ :osfamily => 'Unsupported' }}

it 'we fail' do
  expect { subject }.to raise_error(/Unsupported platform: Unsupported/)
end

To this:

context 'on unsupported distributions' do let(:facts) {{ :osfamily => 'Unsupported' }}

it 'we fail' do
  should compile.and_raise_error(/Unsupported platform: Unsupported/)
end
相关阅读:
Top