2014-06-17 4 views
4

Привет, я делаю тест с chefspec, и я нахожу следующую ошибку.Файл-заглушка открыт в chefspec

в /cookbook/wordpress/recipes/default.rb

wp_secrets = Chef::Config[:file_cache_path] + '/wp-secrets.php' 

if File.exist?(wp_secrets) 
    salt_data = File.read(wp_secrets) 
else 
    require 'open-uri' 
    salt_data = open('https://api.wordpress.org/secret-key/1.1/salt/').read 
    open(wp_secrets, 'wb') do |file| 
    file << salt_data 
    end 
end 

Когда я бегу ChefSpec, я получаю:

1) wordpress::default on Centos 6.5 includes depends recipes 
    Failure/Error: end.converge('wordpress::default') 
    Errno::ENOENT: 
    No such file or directory @ rb_sysopen - /var/chef/cache/wp-secrets.php 

    # /tmp/d20140617-408-1rx47um/cookbooks/wordpress/recipes/default.rb:77:in `from_file' 
    # ./spec/centos/default_spec.rb:10:in `block (2 levels) in <top (required)>' 
    # ./spec/centos/default_spec.rb:20:in `block (2 levels) in <top (required)>' 

Когда я добавляю окурок в default_spec.rb

before do 
    File.stub(:exist?) 
    .with("#{Chef::Config[:file_cache_path]}/wp-secrets.php") 
    .and_return(true) 
end 

Я получаю эту ошибку:

1) wordpress::default on Centos 6.5 includes depends recipes 
    Failure/Error: end.converge('wordpress::default') 
    <File (class)> received :exist? with unexpected arguments 
     expected: ("/var/chef/cache/wp-secrets.php") 
      got: ("/tmp/d20140617-479-1gqb2lc/cookbooks/chefignore") 
     Please stub a default value first if message might be received with other args as well. 
    # ./spec/centos/default_spec.rb:10:in `block (2 levels) in <top (required)>' 
    # ./spec/centos/default_spec.rb:20:in `block (2 levels) in <top (required)>' 

ответ

10

Вы должны окурок оригинальный вызов:

В ChefSpec 3:

File.stub(:exist?).and_call_original 
File.stub(:exist?).with('/path/to/file').and_return('...') 

В ChefSpec 4:

allow(File).to receive(:exist?).and_call_original 
allow(File).to receive(:exist?).with('/path/to/file').and_return('...') 
+1

Это помогло мне удалить мои предупреждения устаревания для RSpec, а также. Благодаря! – Omni