无法解决远程文件资源的rspec问题

时间:2017-03-29 15:26:27

标签: ruby tdd chef chef-recipe chefspec

我尝试为我的一个remote_file资源编写rspec。但我没有成功 我的概念是使用remote_file它应该下载一个zip文件的远程文件。在远程下载后,rspec的期望值更高。

这是我的资源声明:

remote_file zip_file_location do
  source http://google.com
  mode '0754'
  action :create
end​

这是我的rspec测试:

it 'creates a remote_file ' do
    expect(chef_run).to create_remote_file(::File.join(Chef::Config[:file_cache_path], 'sonarqube-5.6.6.zip'))
end

2 个答案:

答案 0 :(得分:0)

这是您的资源

remote_file 'sonarqube-5.6.6.zip' do
 source 'http://google.com/'
 action :create
 mode 00755
end

这是您的资源的rspec

it 'creates a remote_file ' do
  expect(chef_run).to create_remote_file('sonarqube-5.6.6.zip').with(
      source: "http://google.com",
      mode: 00755
  )
end

如果您想使用attributes / default.rb中的配置值来保存已保存文件的位置,则必须在rspec文件中对此进行模拟:

describe 'lecturio_ds::webfrontend' do
  context 'When all attributes are default, on an unspecified platform'  do
  let(:chef_run) do
    ChefSpec::SoloRunner.new do |node|
      node.set['file_cache_path'] = '/tmp'
    end.converge(described_recipe)
  end
# place for your spec
end

之后您可以验证create_remote_file('/tmp/sonarqube-5.6.6.zip')

我不明白你在rspec文件File.join中使用的原因。

答案 1 :(得分:0)

由于一些原因,这对我有用,我通过提供远程文件的路径来修改我的代码,这有助于我解决rspec以确切地期望zip文件位置

这帮助我解决了rspec



remote_file 'Download zip file'do
Path zip_file_location   
source http://google.com
mode '0754'
action :create
end​


it 'creates a remote_file ' do
expect(chef_run).to create_remote_file('Download remote file')
end