【问题标题】:Rspec block (3 levels) in <top (required)> error<top (required)> 错误中的 Rspec 块(3 级)
【发布时间】:2015-11-30 05:38:00
【问题描述】:

提交项目时,我在 travis ci 中收到以下错误:

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

这里是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

如果我正确理解了block (3 levels) in &lt;top (required)&gt;,那么第 5 行和第 11 行之间的缩进是错误的,但它对我来说似乎是正确的。对此的任何和所有帮助将不胜感激,因为这是我第一次与 rspec 打交道,我正在努力学习。

编辑 我自己解决了这个问题。我从这里更改了我的代码:

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

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

到这里:

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

    it 'we fail' do
      should compile.and_raise_error(/Unsupported platform: Unsupported/)
    end
  end

而且它现在似乎正在过去。我想我的问题是:这是正确的还是我错误地通过了测试?

【问题讨论】:

  • 不知道为什么这会被否决。答案不是很明显,我不得不搜索 git 问题来找到解决方案。

标签: ruby rspec travis-ci


【解决方案1】:

通过更改以下内容解决了这个问题:

context '在不支持的发行版上' 做 let(:facts) {{ :osfamily => '不支持' }}

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

到这里:

context '在不支持的发行版上' 做 let(:facts) {{ :osfamily => '不支持' }}

it 'we fail' do
  should compile.and_raise_error(/Unsupported platform: Unsupported/)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多