代码之家  ›  专栏  ›  技术社区  ›  nnyby

用RSpec测试回形针文件的上传

  •  6
  • nnyby  · 技术社区  · 15 年前

    validates_attachment_presence

    所以现在我用规范中的这些属性创建我的模型来尝试关闭它:

    @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 回复  |  直到 11 年前
        1
  •  11
  •   0m3r    10 年前

    如果模型 has_attached_file :pic ,你应该能指出 pic 属性在某个文件和所有应该是花花公子。

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