使用RSpec测试Paperclip文件上载

nny*_*yby 6 unit-testing rspec ruby-on-rails paperclip

我并不真正关心测试文件上传,但由于我validates_attachment_presence在我的模型中有等等,因此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
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用.

我发现了这个:http://fr.ivolo.us/posts/mocking-paperclip-with-rspec 所以我尝试过这样的事情:

Post.should_receive(:save_attached_files).and_return(true)
Run Code Online (Sandbox Code Playgroud)

哪个也行不通.我该如何安抚RSpec?

the*_*eIV 10

如果是模型has_attached_file :pic,您应该只能将pic属性指向某个文件,并且所有文件都应该是花花公子.

意思是这样的 @attr = { :pic => File.open(File.join(Rails.root, 'spec', 'fixtures', 'file.png')) }