Ruby-on-rails 使用 Rspec (Rails) 单元测试回形针上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2256012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unit testing paperclip uploads with Rspec (Rails)
提问by stephenmurdoch
Total Rspec noob here. Writing my first tests tonight.
总 Rspec 菜鸟在这里。今晚写我的第一个测试。
I've got a model called Image. Using paperclip I attach a file called photo. Standard stuff. I've run the paperclip generator and everything works fine in production and test modes.
我有一个名为 Image 的模型。使用回形针,我附加了一个名为 photo 的文件。标准的东西。我已经运行了回形针生成器,并且在生产和测试模式下一切正常。
Now I have a spec file called image.rb and it looks like this (it was created by ryanb's nifty_scaffold generator):
现在我有一个名为 image.rb 的规范文件,它看起来像这样(它是由 ryanb 的 nifty_scaffold 生成器创建的):
require File.dirname(__FILE__) + '/../spec_helper'
describe Image do
it "should be valid" do
Image.new.should be_valid
end
end
This test fails and I realise that it's because of my model validations (i.e. validates_attachment_presence)
这个测试失败了,我意识到这是因为我的模型验证(即 validates_attachment_presence)
The error that I get is:
我得到的错误是:
Errors: Photo file name must be set., Photo file size file size must be between 0 and 1048576 bytes., Photo content type is not included in the list
So how do I tell rspec to upload a photo when it runs my test?
那么我如何告诉 rspec 在运行我的测试时上传照片?
I'm guessing that it's got somethign to do with fixtures.... maybe not though. I've tried playing around with them but not having any luck. For the record, I've created a folder called images inside my fixtures folder and the two files I want to use in my tests are called rails.png and grid.png)
我猜这与固定装置有关……但也许不是。我试过和他们一起玩,但没有任何运气。作为记录,我在我的装置文件夹中创建了一个名为图像的文件夹,我想在我的测试中使用的两个文件称为 rails.png 和 grid.png)
I've tried doing the following:
我试过做以下事情:
it "should be valid" do
image = Image.new :photo => fixture_file_upload('images/rails.png', 'image/png').should be_valid
# I've also tried adding stuff like this
#image.stub!(:has_attached_file).with(:photo).and_return( true )
#image.stub!(:save_attached_files).and_return true
#image.save.should be_true
end
But rspec complains about "fixture_file_upload" not being recognised... I am planning to get that Rspec book. And I've trawled around the net for an answer but can't seem to find anything. My test database DOES get populated with some data when I remove the validations from my model so I know that some of it works ok.
但是 rspec 抱怨“fixture_file_upload”没有被识别......我打算得到那本 Rspec 书。我在网上四处寻找答案,但似乎找不到任何东西。当我从我的模型中删除验证时,我的测试数据库确实填充了一些数据,所以我知道其中一些工作正常。
Thanks in advance,
提前致谢,
EDIT:
编辑:
images.yml looks like this:
images.yml 看起来像这样:
one:
name: MyString
description: MyString
two:
name: MyString
description: MyString
回答by chrisdinn
This should work with Rails 2.X:
这应该适用于 Rails 2.X:
Image.new :photo => File.new(RAILS_ROOT + '/spec/fixtures/images/rails.png')
As of Rails 3, RAILS_ROOTis no longer used, instead you should use Rails.root.
从 Rails 3 开始,RAILS_ROOT不再使用,而是应该使用Rails.root.
This should work with Rails 3:
这应该适用于 Rails 3:
Image.new :photo => File.new(Rails.root + 'spec/fixtures/images/rails.png')
Definitely get the RSpec book, it's fantastic.
一定要买 RSpec 书,太棒了。
回答by TJ Singleton
Rails.root is a pathname object so you can use it like this:
Rails.root 是一个路径名对象,所以你可以像这样使用它:
Image.new :photo => Rails.root.join("spec/fixtures/images/rails.png").open
Edit - probably does not work in Rails 3...
编辑 - 可能在 Rails 3 中不起作用......
- see answer by @Paul Rosania
- 见@Paul Rosania 的回答
回答by Paul Rosania
In case anyone else finds this via Google, RAILS_ROOT is no longer valid in Rails 3.0. That line should read:
万一其他人通过 Google 找到了这个,RAILS_ROOT 在 Rails 3.0 中不再有效。该行应为:
Image.new :photo => File.new(Rails.root + 'spec/fixtures/images/rails.png')
(Note the lack of leading slash!)
(注意缺少前导斜线!)
回答by paul.belt
I use the multipart_body gem in my integration tests. Its a bit truer to BDD than testing.
我在集成测试中使用 multipart_body gem。与测试相比,它更适合 BDD。
http://steve.dynedge.co.uk/2010/09/19/multipart-body-a-gem-for-working-with-multipart-data/
http://steve.dynedge.co.uk/2010/09/19/multipart-body-a-gem-for-working-with-multipart-data/
With respect to rspec and paperclip, the has_attached_file :photo directive creates a virtual attribute of sorts i.e. :photo ... when you assign a file or a path to photo, paperclip takes over, stores the file, optionally does processing on it e.g. auto-create thumbnails, import a spreadsheet, etc. You aren't telling rspec to test paperclip. You are invoking code and telling rspec what the results of that code -should- be.
关于 rspec 和回形针, has_attached_file :photo 指令创建了一个虚拟属性,即 :photo ...当您为照片分配文件或路径时,回形针接管,存储文件,可选择对其进行处理,例如自动- 创建缩略图,导入电子表格等。您不是告诉 rspec 测试回形针。您正在调用代码并告诉 rspec 该代码的结果应该是什么。
In $GEM_HOME/gems/paperclip-2.3.8/README.rdoc, about 76% of the way through the file under ==Post Processing (specifically lines 147 and 148):
在 $GEM_HOME/gems/paperclip-2.3.8/README.rdoc 中,大约 76% 通过 ==Post Processing 下的文件(特别是第 147 和 148 行):
---[ BEGIN QUOTE ]--- NOTE: Because processors operate by turning the original attachment into the styles, no processors will be run if there are no styles defined. ---[ END QUOTE ]---
---[ 开始引用]--- 注意:由于处理器通过将原始附件转换为样式来运行,因此如果没有定义样式,则不会运行任何处理器。---[结束报价]---
Reading the code, you'll see support :original ... does your has_attached_file define a style?
阅读代码,您会看到 support :original ... 您的 has_attached_file 是否定义了样式?
I use a generic ":styles => { :original => { :this_key_and => :this_value_do_not_do_anything_unless_a_lib_paperclip_processors__foo_dot_rb__does_something_with_them } }" ... just to get paperclip to move the file from some temp directory into my has_attached_file :path
我使用通用的“:styles => { :original => { :this_key_and => :this_value_do_not_do_anything_unless_a_ lib_paperclip_processors__foo_dot_rb__does_something_with_them } }” ...只是为了让回形针将文件从某个临时目录移动到我的 has_attached_ 文件中
One would think that would be default or more obvious in the docs.
人们会认为这在文档中是默认的或更明显的。

