0

I tried to write rspec for one of my remote_file resource. But I am unsuccessful to make it.
My concept is using remote_file it should download a remote file which is a zip file. What happen is rspec is expecting more after It downloaded remotely.

This is my resource declaration:

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

This is my rspec test:

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
gsone
  • 1,188
  • 8
  • 25

2 Answers2

0

This is your resource

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

and this is rspec for your resource

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

If you want to have configuration value from attributes/default.rb for location of your saved file you must mock this in your rspec file:

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

and after that you can verify it create_remote_file('/tmp/sonarqube-5.6.6.zip').

I don't understand why you use in your rspec file File.join there.

gsone
  • 1,188
  • 8
  • 25
  • @gig I gave the path of the remote file where it needs to be installed –  Mar 30 '17 at 00:43
  • Rspec is like unit testing for your code, there is no real file ended in your desire directory. – gsone Mar 31 '17 at 06:15
0

For few reasons this worked me, I modified my code by giving path for remote file which helped me to solve rspec to expect exactly the zip file location

this helped me to solve 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