【问题标题】:Chefspec defining local variable or attributesChefspec 定义局部变量或属性
【发布时间】:2018-05-29 11:30:59
【问题描述】:

我正在编写chefspec测试,我有以下代码

   recipe


execute 'stop Server' do
  user '123'
  group 'ad'
  live_stream true
  command "/udd001/app/WebSphere/AppServer/profiles/AppSrv01/bin/stopServer.sh $server -username wasserver -password #{password}"
  only_if { ::File.exist?('/mypath/'+variable[:myserver) }


     spec

require 'spec_helper'

describe 'cookbook::myserver' do
  before do
    allow(File).to receive(:exist?).and_call_original
    allow(File).to receive(:exist?).with('/mypath/'+variable[:appserver_profile]).and_return(true)
  end

  context 'When all attributes are default, on Ubuntu 16.04' do
    let(:chef_run) do
      runner = ChefSpec::ServerRunner.new(platform: 'ubuntu', version: '16.04')
      runner.converge(described_recipe)
    end

    it 'converges successfully' do
      expect { chef_run }.to_not raise_error
    end

    it 'stop Server' do
      expect(chef_run).to run_execute('myserver').with(
        user: '123',
        group: 'ad',
        live_stream: true)
    end
  end

end

由于未定义局部变量“变量”而失败

【问题讨论】:

    标签: ruby-on-rails rspec chef-infra chefspec


    【解决方案1】:

    你不能在你的规范中使用本地变量(我假设variable 在你的食谱代码中进一步定义,但你实际上并没有显示)在你的规范中。事实上,您不应该这样做,因为测试的全部目的是与已知输出进行比较,所以您要做的是通过并计算出完整路径应该是什么,然后将其放入测试中。测试代码在某种程度上不受“不要重复自己”规则的约束,因为重点是通过重复来信任。

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 2019-11-07
      • 2017-01-26
      • 2018-04-19
      • 2015-01-01
      • 2013-12-17
      相关资源
      最近更新 更多