2014-02-16 39 views
0

我有一个厨师食谱,它将ubutunu用户(使用amazon AMI am的默认用户)授权密钥文件复制到新创建的用户。在ChefSpec中嘲笑文件内容

ubuntu_public_key_file = "/home/ubuntu/.ssh/authorized_keys" 
file "#{new_user_homedir}/.ssh/authorized_keys" do 
    owner new_user 
    group new_user_group 
    mode "0600" 
    content IO.read(ubuntu_public_key_file) 
end 

我试用了chefspec,我想测试一下。我想嘲笑ubuntu_public_key_file及其内容的存在。任何意见都表示赞赏!

+0

数据包将是一个更好的地方,让你的公钥 –

回答

0

您需要Rspec存根:

describe 'cookbook::recipe' do 
    let(:content) do 
    "CUSTOM SSH KEY CONTENT HERE" 
    end 

    before do 
    IO.stub(:read).with('/home/ubuntu/.ssh/authorized_keys').and_return(content) 
    end 
end 
+0

感谢您的建议,但我得到了下面的错误 预期:(“/家/ Ubuntu的/ .ssh/authorized_keys“) got:(”/home/kenji/.rvm/gems/[email protected]/gems/fauxhai-2.0.1/lib/fauxhai/platforms/ubuntu/12.04 .json“)” 如果消息可能与其他参数一起收到,请先存储默认值 我不认为我想存根默认行为,但只是这个file.read。关于如何修复的任何建议这个? – DheeDaOx

+0

阅读关于RSpec stubbing,或谷歌的错误 – sethvargo

+0

谢谢!此链接http://stackoverflow.com/questions/9972964/rspec-stubbing-method-for-only-specific-参数帮助:-) – DheeDaOx