2010-07-28 45 views
6

我並不在乎測試文件上傳,但因爲我有validates_attachment_presence等,在我的模型中,rspec在抱怨。測試用RSpec上傳回形針文件

所以現在我創造我與規範這些屬性模型,試圖關閉它了:

@attr = { 
    :name => "value for name", 
    :title => "value for title", 
    :content => "value for content", 
    :pic_file_name => "example.jpg", 
    :pic_content_type => "image/jpg", 
    :pic_file_size => "8192", 
    :pic_updated_at => nil 
} 

這是不行的,雖然。

我發現這一點:http://fr.ivolo.us/posts/mocking-paperclip-with-rspec 所以,我想是這樣的:

Post.should_receive(:save_attached_files).and_return(true) 

這也不起作用。我如何安撫RSpec?

+1

可能重複[單元測試回形針上傳使用RSpec(滑軌)(http://stackoverflow.com/questions/2256012/unit-testing-paperclip-uploads-with-rspec-導軌) – 2011-09-19 07:32:25

回答

10

如果模型has_attached_file :pic,你應該能夠只是指出一些文件pic屬性,都應該是花花公子。

含義類似@attr = { :pic => File.open(File.join(Rails.root, 'spec', 'fixtures', 'file.png')) }

+0

這用於爲我工作,但從2.5.0升級到回形針3.1.4時似乎不工作。 – 2012-08-24 10:53:49

+0

@ DavidN.Welton你會得到什麼樣的錯誤(如果有的話)?我沒有很多Paperclip 3的經驗。 – theIV 2012-08-24 14:01:28

+0

它做了什麼修正:'mymodel.attachedimage = File.open(....)'而不是'attachchedimage =「file/location/foo.jpg」 – 2012-08-24 20:24:49