2013-05-11 16 views
6

Próbuję przetestować moją aplikację z ogórkiem i kapibarą. mam następującą definicję kroku:Capybara FactoryGirl Carrierwave nie może dołączyć pliku

Given(/^I fill in the create article form with the valid article data$/) do 
    @article_attributes = FactoryGirl.build(:article) 
    within("#new_article") do 
    fill_in('article_title', with: @article_attributes.title) 
    attach_file('article_image', @article_attributes.image) 
    fill_in('article_description', with: @article_attributes.description) 
    fill_in('article_key_words', with: @article_attributes.key_words) 
    fill_in('article_body', with: @article_attributes.body) 
    end 

Mój artykuł fabryka wygląda następująco:

FactoryGirl.define do 
    factory :article do 
    sequence(:title) {|n| "Title #{n}"} 
    description 'Description' 
    key_words 'Key word' 
    image { File.open(File.join(Rails.root, '/spec/support/example.jpg')) } 
    body 'Lorem...' 
    association :admin, strategy: :build 
    end 
end 

I to jest mój plik uploader:

# encoding: UTF-8 
class ArticleImageUploader < CarrierWave::Uploader::Base 
    storage :file 
    def store_dir 
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
    end 
    def extension_white_list 
    %w(jpg jpeg gif png) 
    end 
end 

Ale za każdym razem uruchomić ten scenariusz Otrzymuję komunikat ERROR:

Given I fill in the create article form with the valid article data # features/step_definitions/blog_owner_creating_article.rb:1 
     cannot attach file, /uploads/article/image/1/example.jpg does not exist (Capybara::FileNotFound) 
     ./features/step_definitions/blog_owner_creating_article.rb:5:in `block (2 levels) in <top (required)>' 
     ./features/step_definitions/blog_owner_creating_article.rb:3:in `/^I fill in the create article form with the valid article data$/' 
     features/blog_owner_creating_article.feature:13:in `Given I fill in the create article form with the valid article data' 

Stwierdziłem również, że FactoryGirl zwraca image:nil po uruchomieniu FactoryGirl.build(:article) w mojej konsoli testowej rails.

Czy ktoś może mi wyjaśnić, co robię źle, proszę?

Odpowiedz

10

Trzeba przejść ścieżkę bezpośrednio:

attach_file('article_image', File.join(Rails.root, '/spec/support/example.jpg')) 

Co tu się dzieje jest to, że attach_file oczekuje ciąg, a nie do przesyłania CarrierWave. Po jego przesłaniu program ładujący (@article_attributes.image), attach_file wywołuje Uploader#to_s, który wywołuje Uploader#path. Ponieważ nie zapisałeś jeszcze artykułu, ścieżka, w której zostanie umieszczony przesłany obraz, jest nieprawidłowa.

Należy również zauważyć, że wywoływanie zmiennej @article_attributes jest mylące, ponieważ jest to w rzeczywistości obiekt całego artykułu, a nie tylko skrót. Jeśli tego chcesz, możesz spróbować FactoryGirl.attributes_for(:article).

+0

Thats za odpowiedź! Próbowałem użyć '@article_attributes = FactoryGirl.attributes_for (: article)' tak jak powiedziałeś. Ale '@artykle_attributes [: image]' zwraca '# '. Czy istnieje sposób przekształcenia tego na ścieżkę z ciągiem bezpośrednim? ' –

+0

' @article_attributes [: image] .path'? Byłby to szalony świat, gdyby nie było sposobu [aby uzyskać ścieżkę z obiektu File] (http://ruby-doc.org/core-2.0/File.html#method-i-path). – Taavo

+0

DZIĘKI DUŻO! Teraz wszystko działa. –

Powiązane problemy