2012-01-25 17 views
16

Aşağıda, dosya yükleme için test kodum var. Ben RSpec spec çalıştırdığınızda fixture_file_upload'ta dosya mevcut değil {dosya} hatası var

describe "file process" do 
before(:each) do 
    # debugger 
    @file = fixture_file_upload('test.csv', 'text/csv') 
end 

it "should be able to upload file" do 
    post :upload_csv, :upload => @file 
    response.should be_success 
end 
end 

Ancak, ben yerlerde bir sürü googled var ama hala bunun arkasında nedeni ne olduğunu bulamadık

Failure/Error: @file = fixture_file_upload('test.csv', 'text/csv') 
RuntimeError: 
    test.csv file does not exist 
# ./spec/controllers/quotation_controller_spec.rb:29:in `block (3 levels) in <top (required)>' 

altına bana hata oluşturdu. Herhangi bir fikir?

+7

'a yerleştirilmelidir. SO'dan bir çözüm bulundu [http://stackoverflow.com/questions/3966263/attachment-fu-testing-in-rails-3](http:// stackoverflow.com/questions/3966263/attachment-fu-testing-in-rails-3). Görünüşe göre, fixture_file_upload ile bu satırı değiştirmeliyim: @file = Rack :: Test :: UploadedFile.new (Rails.root.join ('spec/fikstür/test.csv'), 'text/csv') ' – hanchee

+0

fixture_file_upload Rails 3.1 ile çalışmıyor? – hanchee

+1

3.2 – plainjimbo

cevap

26

Fixture_file_upload temelde hala çalışır/Spec/fikstürler/dosyaları {Rails.root} altındaki dosyayı koymak zorunda. Sadece kendi spec_helper.rb dosyasında emin fikstürler yolunu yapmak gerekir doğru ActionDispath::TestProcess içerecek şekilde spec/fixtures yoluna & ayarlı uncommented & geçerli:

RSpec.configure do |config| 
    config.include ActionDispatch::TestProcess 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    ... 

Ve siz testlerde dosyası belirterek nerede, önce mutlaka sizin '/' gibi, aşağıdaki örnekle dosya adı:

describe "POST /subscriber_imports" do 
    let(:file) { { :file => fixture_file_upload('/files/data.csv', 'text/csv') } } 
    subject { post :create, :subscriber_import => file } 
    ... 
end 

dosyasına mutlak yol config.fixture_path belirtilen ana yol artı fixture_file_upload işlev çağrısında belirtilen göreli bir yoldur. Bu nedenle, bu örnekte, file.csv#{::Rails.root}/spec/fixtures/files/data.csv

+3

Ayrıca, ActionDispatch şunları içerir: : TestProcess'. Bu özellikle benim için de sorunu düzeltildi – FeifanZ

+0

'içerir ActionDispatch :: TestProcess 'de benim için de ne olduğunu. Garip kısım, diğer rspec testlerinde 'fixture_file_upload '' yi kullanmış olmamızdı, fakat yeni bir tane için başarısız oldu. Bu yeni spesifikasyonun bir modülde kapsandığı görülmüştür. – nrowegt

İlgili konular