我正在尝试使用回形针为带有图片的模型编写测试。我使用的是默认测试框架,没有 shoulda 或 rspec。在这种情况下,我应该如何测试呢?我真的应该上传文件吗?我应该如何将文件添加到夹具?
最佳答案
将文件添加到模型非常简单。例如:
@post = Post.new
@post.attachment = File.new("test/fixtures/sample_file.png")
# Replace attachment= with the name of your paperclip attachment
在那种情况下,您应该将文件放入您的 test/fixtures
目录。
我通常在我的test_helper.rb中做一个小 helper
def sample_file(filename = "sample_file.png")
File.new("test/fixtures/#{filename}")
end
然后
@post.attachment = sample_file("filename.txt")
如果你使用类似 Factory Girl 的东西而不是固定装置,这变得更加容易。
关于ruby-on-rails - rails 中的单元测试 - 带回形针的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1895092/