【问题标题】:Testing Paperclip file uploading with RSpec使用 RSpec 测试回形针文件上传
【发布时间】:2010-07-28 23:47:52
【问题描述】:

我并不真正关心测试文件上传,但由于我的模型中有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
}

不过,这不起作用。

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

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

这也不起作用。如何安抚 RSpec?

【问题讨论】:

标签: ruby-on-rails unit-testing rspec paperclip


【解决方案1】:

如果模型为has_attached_file :pic,您应该能够将pic 属性指向某个文件,并且一切都应该是花花公子。

意思类似于@attr = { :pic => File.open(File.join(Rails.root, 'spec', 'fixtures', 'file.png')) }

【讨论】:

  • 这曾经对我有用,但在从 2.5.0 升级到回形针 3.1.4 时似乎不起作用。
  • @DavidN.Welton 你得到什么样的错误(如果有的话)?我对 Paperclip 3 没有太多经验。
  • 解决了什么问题:mymodel.attachedimage = File.open(....) 而不是 attachedimage = "file/location/foo.jpg"
  • @DavidN.Welton 你有没有想过这个问题?我也有同样的问题。
  • @JasonSwett 看到上面的评论 - 你必须传递一个文件句柄而不是文件名。
猜你喜欢
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 2014-05-23
相关资源
最近更新 更多