我试图用回形针为带图片的模型编写一个测试。我正在使用测试框架的默认,没有shoulda或rspec。在这种情况下,我应该如何测试它?我应该真的上传一个文件吗?我应该如何将文件添加到灯具?在轨道单元测试 - 带回形针的模型
33
A
回答
68
将文件添加到模型非常简单。例如:
@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中
def sample_file(filename = "sample_file.png")
File.new("test/fixtures/#{filename}")
end
然后
@post.attachment = sample_file("filename.txt")
如果你使用类似Factory Girl,而不是灯具这就变得更容易。
16
这是Rspec的,但可以很容易地切换
before do # setup
@file = File.new(File.join(RAILS_ROOT, "/spec/fixtures/paperclip", "photo.jpg"), 'rb')
@model = Model.create!(@valid_attributes.merge(:photo => @file))
end
it "should receive photo_file_name from :photo" do # def .... || should ....
@model.photo_file_name.should == "photo.jpg"
# assert_equal "photo.jpg", @model.photo_file_name
end
由于回形针是相当行之有效的,我平时不关注的“上传”行为太多了,除非我做一些不寻常的东西。但我会尝试更多地关注确保附件的配置与其所属的型号相符,以满足我的需求。
it "should have an attachment :path of :rails_root/path/:basename.:extension" do
Model.attachment_definitions[:photo][:path].should == ":rails_root/path/:basename.:extension"
# assert_equal ":rails_root/path/:basename.:extension", Model.attachment_definitions[:photo][:path]
end
所有的好东东都可以在Model.attachment_definitions
找到。
1
我使用FactoryGirl,设置模型。
#photos.rb
FactoryGirl.define do
factory :photo do
image File.new(File.join(Rails.root, 'spec', 'fixtures', 'files', 'testimg1.jpg'))
description "testimg1 description"
end # factory :photo
end
然后
# in spec
before(:each) { @user = FactoryGirl.create(:user, :with_photo) }
在回形针附件指定其将被保存,即
...
the_path= "/:user_id/:basename.:extension"
if Rails.env.test?
the_path= ":rails_root/tmp/" + the_path
end
has_attached_file :image, :default_url => ActionController::Base.helpers.asset_path('missing.png'),
:path => the_path, :url => ':s3_domain_url'
Paperclip.interpolates :user_id do |attachment, style|
attachment.instance.user_id.to_s
end
...
然后同时测试attachment_definitions(如建议kwon)和Dir.glob检查文件是否已保存
it "saves in path of user.id/filename" do
expect(Dir.glob(File.join(Rails.root, 'tmp', @user.id.to_s, @user.photo.image.instance.image_file_name)).empty?).to be(false)
end
这样,我敢肯定它的执行正确的directy /路径创建等
相关问题
- 1. 用轨道上的红宝石回形针模型播种
- 2. 环回模型的单元测试
- 3. 回形针::错误::在轨道4,5
- 4. 使用附件的功能的轨道单元测试
- 5. 轨道上的回形针红宝石
- 6. Rails:如何知道单元测试中哪个测试模型?
- 7. 轨道 - 回形针文件名
- 8. django模型单元测试
- 9. Django模型单元测试
- 10. 轨,SRP,和单元测试
- 11. 轨+回形针+ plupload
- 12. 如何在轨道上的红宝石单元测试
- 13. 带回形针的每个模型的多个附件
- 14. Rails 3 - 回形针带回形针
- 15. 轨道模型
- 16. 回形针和功能测试问题(导轨)
- 17. 轨单元测试错误:“测试是不是一个模块”
- 18. 简单的SSH测试轨道
- 19. Phalcon - 模拟单元测试模型
- 20. Mongomapper - 单元测试与轨道上的应该2.3.5
- 21. 嵌套模型形式轨道3
- 22. 测试记录轨道3.1
- 23. 带相册的回形针
- 24. RSpec和测试轨道Gem
- 25. 带有form_tag的回形针没有相应的模型
- 26. 用回形针悬挂的Rspec测试
- 27. 轨回形针tinypng宝石
- 28. swfupload +回形针+导轨3
- 29. 使用磁带/ ES6单元测试测试AMD模块?
- 30. 测试用一个单一个轨道模型的验证方法
它提供:NoMethodError: 未定义的方法'附件=” – CanCeylan 2012-12-25 12:51:58
@CanCeylan'attachment'是通用的,比如'foo' ;您应该将其替换为您在模型上添加回形针附件时使用的任何名称。 – 2013-02-01 19:34:15
@RicoJones谢谢,在帖子中补充说明。 – 2013-02-01 23:01:46