2014-07-20 41 views
5

我在嘗試使用Capybara的attach_file方法測試圖片上傳是否適用於回形針時遇到問題。在使用Capybara attach_file方法測試上傳文件時遇到問題

rspec的返回這些錯誤如下: 失敗:

1) Upload Background Image cannot upload a background image 
Failure/Error: page.attach_file('#artist_background_image', Rails.root +   'spec/Fixtures/Snow.jpg') 
Capybara::ElementNotFound: 
    Unable to find file field "#artist_background_image" 
# ./spec/models/artist_spec.rb:65:in `block (2 levels) in <top (required)>' 

我的測試如下所示。我假定選擇文件按鈕是attach_file的選擇器,所以我使用了選擇器'#artist_background_page'的id。

describe 'Upload Background Image', js: true do 
    before :each do 
    p '=================' 
    pp Artist.all 
    @artist = Artist.first || Artist.create(artist_name:'Double Stuff Oreos', email:'[email protected]', password:'letmein') 
    visit "/artists/" + @artist.id.to_s 
    pp @artist.id 
    pp @artist.artist_name 
    p @artist.errors.full_messages 
    click_button 'Log in' 
    fill_in 'Email', with: '[email protected]' 
    fill_in 'Password', with: 'letmein' 
    page.find('#loginButtonModal').click 
    page.find('#addBackgroundImagePrompt').click 
    page.attach_file('#artist_background_image', Rails.root + 'spec/Fixtures/Snow.jpg') 
    p Rails.root + 'spec/Fixtures/Snow.jpg' 
    click_button 'upload' 
    end 

    it "cannot upload a background image", js: true do 
    backgroundURL = @artist.background_image.url.include?('Snow.jpg') 
    p @artist.background_image.url 
    expect(backgroundURL).to be_truthy 
    end 
end 

但是,當我將選擇器更改爲'artist_background_page'而沒有ID'#'符號時。 rspec的給了我不同的錯誤:

...."=================" 
[] 
"is_active_session is called" 
#<Artist id: 1, artist_name: "Double Stuff Oreos", route_name: "double-stuff-oreos",  created_at: "2014-07-20 17:20:48", updated_at: "2014-07-20 17:20:48", password_digest: "$2a$04$vgozdgieklXPjJ9Ri4Cv1e1d/hme0ybNnSEGXrmob5z...", remember_token: nil, email: "[email protected]", description: nil, youtube: nil, twitter: nil, facebook: nil, instagram: nil, hometown: nil, confirmation_code: nil, is_confirmed: nil, background_image_file_name: nil, background_image_content_type: nil, background_image_file_size: nil, background_image_updated_at: nil> 
1 
"Double Stuff Oreos" 
[] 
"is_active_session is called" 
#<Pathname:/Users/bob/rails_projects/audience/spec/Fixtures/Snow.jpg> 
"/background_images/original/missing.png" 
"Hello from update" 
"Logged in!" 

An error occurred in an after hook 
ActiveRecord::RecordNotFound: Couldn't find Artist with 'id'=1 
occurred at /Users/shuo/.rvm/gems/ruby-2.1.2/gems/activerecord- 4.1.4/lib/active_record/relation/finder_methods.rb:320:in `raise_record_not_found_exception!' 

F.... 

Failures: 

1) Upload Background Image cannot upload a background image 
    Failure/Error: expect(backgroundURL).to be_truthy 
    expected: truthy value 
     got: false 
# ./spec/models/artist_spec.rb:74:in `block (2 levels) in <top (required)>' 

在這種情況下,模型的創建,但錯誤說,它不能找到它出於某種原因... 有沒有我可以使用在水豚附加文件的任何其他方法並測試成功上傳?

可能性失敗:

  1. 錯誤路徑文件上傳
  2. 使用錯誤的方法或無效的使用方法
  3. 有毛病服務器

回答

11

此錯誤:

Capybara::ElementNotFound: 
    Unable to find file field "#artist_background_image" 

表明水豚找不到名爲#artist_background_image的字段。您需要做的是按名稱引用文件上傳字段。

假設你有

<input id="artist_background_image" name="file_upload" type="file"> 

那麼你可以參考該領域,如:

page.attach_file('file_upload', Rails.root + 'spec/Fixtures/Snow.jpg') 

就這樣,水豚就知道通過上傳什麼領域。

我知道這是一個遲到的反應,但我認爲這將有助於其他人面臨這個問題。

0

如果您使用simple_form寶石,可能會出現這種情況。

請注意,它會自動爲您的表單元素生成標識符,因此您自己手動標識元素是徒勞的!

您可以通過手動確定一個表單元素來確定這一點,然後在瀏覽器中查看源代碼。你會很難看到你的手動分配的id不在頁面源的任何地方。

有;然而,simple_form遵循id表單元素的命名約定。

object_attribute 

示例波紋管可以解釋這一更(HAML〔實施例):然後

= simple_form_for @user do |f| 
    = f.input :image, as: :file 

simple_form將自動生成的形式要素圖像的id作爲如此(object_attribute):

id="user_image" 

現在,您可以簡單地使用此命名約定來定位測試中的ID。希望這可以幫助。

相關問題