2009-12-12 12 views
33

मैं पेपरक्लिप का उपयोग करके एक तस्वीर के साथ एक मॉडल के लिए एक परीक्षण लिखने की कोशिश कर रहा हूं। मैं परीक्षण फ्रेमवर्क डिफ़ॉल्ट, कोई कंधे या rspec का उपयोग कर रहा हूँ। इस संदर्भ में, मुझे इसका परीक्षण कैसे करना चाहिए? क्या मुझे वास्तव में एक फाइल अपलोड करनी चाहिए? मुझे फ़िक्स्चर में फ़ाइल कैसे जोड़नी चाहिए?रेल में इकाई परीक्षण - पेपरक्लिप के साथ मॉडल

उत्तर

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 की तरह कुछ का उपयोग यह और भी आसान हो जाता है में एक छोटे से सहायक हैं।

+0

यह देता है: NoMethodError: अपरिभाषित विधि 'लगाव = ' – CanCeylan

+2

@CanCeylan' attachment', सामान्य है 'foo' की तरह ; जब आप अपने मॉडल पर पेपरक्लिप अटैचमेंट जोड़ते हैं तो आपको इसे किसी भी नाम से बदलना चाहिए। –

+0

@ रिको जोन्स धन्यवाद, पोस्ट में स्पष्टीकरण जोड़ा गया। –

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 में पाया जा सकता।

+0

पर फ़ाइल पथ अपडेट करते हैं @ valid_attributes क्या है? – CanCeylan

+1

यह मेरे टेस्ट केस में मॉडल बनाने के लिए आवश्यक विशेषताओं का हैश है। आपको अपने परीक्षणों के लिए इसकी आवश्यकता नहीं हो सकती है। – nowk

1

मैं फैक्टरीगर्ल का उपयोग करता हूं, मॉडल सेट अप करता हूं।

#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/पथ से बाहर ले जाने बनाने यकीन है कि आदि

संबंधित मुद्दे