简体   繁体   English

滑轨中的单元测试-带回形针的模型

[英]unit test in rails - model with paperclip

I'm trying to write a test for a model with a picture, using paperclip. 我正在尝试使用回形针为带有图片的模型编写测试。 I'm using the test framework default, no shoulda or rspec. 我正在使用默认的测试框架,没有shoulda或rspec。 In this context, how should I test it? 在这种情况下,我应该如何测试? Should I really upload a file? 我真的应该上传文件吗? How should I add a file to the fixture? 我应该如何向灯具添加文件?

Adding file to a model is dead simple. 将文件添加到模型非常简单。 For example: 例如:

@post = Post.new
@post.attachment = File.new("test/fixtures/sample_file.png")
# Replace attachment= with the name of your paperclip attachment

In that case you should put the file into your test/fixtures dir. 在这种情况下,您应该将文件放入test/fixtures目录。

I usually make a little helper in my test_helper.rb 我通常在test_helper.rb中做一个小帮手

def sample_file(filename = "sample_file.png")
  File.new("test/fixtures/#{filename}")
end

Then 然后

@post.attachment = sample_file("filename.txt")

If you use something like Factory Girl instead of fixtures this becomes even easier. 如果您使用诸如Factory Girl之类的东西来代替灯具,这将变得更加容易。

This is in Rspec, but can be easily switched over 这在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

Since Paperclip is pretty well tested, I usually don't focus too much on the act of "uploading", unless i'm doing something out of the ordinary. 由于Paperclip经过了很好的测试,因此我通常不会过多地关注“上载”操作,除非我做了一些与众不同的事情。 But I will try to focus more on ensuring that the configurations of the attachment, in relation to the model it belongs, meet my needs. 但是,我将尝试着重于确保附件的配置及其所属模型能够满足我的需求。

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

All the goodies can be found in Model.attachment_definitions . 可以在Model.attachment_definitions找到所有的好东西。

I use FactoryGirl, setup the Model. 我使用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

then 然后

 # in spec

before(:each) { @user = FactoryGirl.create(:user, :with_photo) }

In the paperclip attachment specify where its going to be saved ie 在回形针附件中指定要保存的位置,即

...
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

... ...

then test both the attachment_definitions (as suggested by kwon) and the Dir.glob to check the file is saved 然后同时测试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

this way i'm sure its carrying out the correct directy/path create etc 这样我确定它执行正确的方向/路径创建等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM