2014-02-16 44 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