2009-12-12 26 views
33

Bir fotoğrafın modelini kullanarak, ataç kullanarak bir resim yazmaya çalışıyorum. Test çerçevesi varsayılanını kullanıyorum, shoulda veya rspec yok. Bu bağlamda, nasıl test etmeliyim? Gerçekten bir dosya yüklemeli miyim? Fikstüre nasıl dosya eklemeliyim?birim testi - paperclip ile model

cevap

68

Bir modele dosya eklemek son derece basit. Örneğin:

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

Bu durumda size test/fixtures dir içine dosyayı koymak gerekir.

Ben genellikle benim test_helper.rb Sonra

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

yerine fikstür Factory Girl gibi bir şey kullanmak bu da kolay hale gelirse

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

biraz yardımcı olun. Paperclip oldukça iyi, ben genellikle "yükleme" eylemi üzerinde çok fazla odaklanmak yok sınanır yana sana yapıyorum sürece

+0

O verir: NoMethodError: tanımsız yöntem 'eki =' – CanCeylan

+2

@CanCeylan' attachment' 'foo' gibi geneldir ; Modelinize bir Paperclip eklentisi eklediğinizde kullandığınız adla değiştirmelisiniz. –

+0

@RicoJones teşekkürler, sonrası açıklama ekledi. –

16

Bu RSpec olduğunu, ancak kolayca olabilir,

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 

geçilir sıradan bir şey. Ancak, ekin konfigürasyonlarının, ait olduğu model ile ilgili olarak, gereksinimlerimi karşıladığından emin olmaya çalışacağım.

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 

tüm güzellikler Model.attachment_definitions bulunabilir.

+0

@valid_attributes nedir? – CanCeylan

+1

Sadece test durumumda model oluşturmak için gerekli niteliklerin bir karmaşası. Kendi testleriniz için buna ihtiyacınız olmayabilir. – nowk

1

FactoryGirl'i kullanıyorum, Modeli ayarlayın.

#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 

sonra ataç eki olarak

# in spec 

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

onun yani

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

kaydedilmiş olacak yeri belirtin ...

sonra

(attachment_definitions hem sınamak önerdiği gibi kwon) ve dosyayı kontrol etmek için Dir.glob kaydedildi

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 

i onun oluşturmak doğru directy/yolu yürüten eminim bu şekilde vb