4

您好正在使用 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)>'
4

1 回答 1

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('...')
于 2014-06-17T19:10:58.690 回答