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
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.
@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
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.
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
- 1. Oracle ile birim testi
- 2. Temel Birim Testi ve Birim Testi
- 3. MSTest - StackOverflowException ile birim testi
- 4. JMS (ActiveMQ) ile birim testi
- 5. Birim testi
- 6. symfony2 birim testi doğrulama
- 7. C++ Birim Testi
- 8. CXF birim testi
- 9. ASP.NET Birim Testi
- 10. AspectJ önerisi, birim testi ile yapılmıyor
- 11. SimplyVBUnit ile VB 6'da Birim Testi
- 12. MonoDevelop ile birim testi nasıl hata ayıklanır?
- 13. asp.net mv'de birim testi ile yönlendirmeyi doğrulayın
- 14. Birim testi özel sınıflar
- 15. Otomatik C birim testi
- 16. Birim testi Activity.startService() çağrısı?
- 17. Vbscript otomatik/birim testi
- 18. Birim testi NHibernate UserTypes
- 19. Birim testi RavenDb sorguları
- 20. Birim Testi CacheManager
- 21. Birim Testi Doctrine ODM
- 22. yasemin parametrelenmiş birim testi
- 23. Birim Testi ASP.net Page_Load
- 24. Birim testi nasıl tanımlanabilir?
- 25. TaskScheduler.FromCurrentSynchronizationContext - birim testi
- 26. Birim Testi ve Log4net
- 27. Javascript Sandbox birim testi
- 28. Silverlight 4 Birim Testi
- 29. Denetleyici birim testi, thin3
- 30. Birim testi Python'da Teoriler?
O verir: NoMethodError: tanımsız yöntem 'eki =' – CanCeylan
@CanCeylan' attachment' 'foo' gibi geneldir ; Modelinize bir Paperclip eklentisi eklediğinizde kullandığınız adla değiştirmelisiniz. –
@RicoJones teşekkürler, sonrası açıklama ekledi. –