[英]How to test a file attach with rspec in a rails project?
我在視圖源中放置了一個文件上傳標記:
<%= form_tag(confirm_path, method: :post, multipart: true) do %>
<%= file_field_tag 'my_file' %>
<button type='submit'>Submit</button>
<% end %>
我想測試用戶是否選擇文件並單擊提交按鈕,然后重定向到下一頁。 這是我的規格來源:
require 'rails_helper'
feature 'MyTest', type: :feature do
describe 'File test' do
before do
visit index_path
end
it 'If select a file, then go to next page' do
attach_file 'my_file', my_real_file_path # I am sure this file exists
click_button 'Submit'
expect(page).to have_content('Confirm Page')
end
end
end
但是在運行此規范測試后,它說:
Failure/Error: attach_file 'my_file', my_real_file_path
Capybara::ElementNotFound:
Unable to find file field 'my_file'
這通常是因為實際的文件字段被隱藏並覆蓋有“按鈕”或某些JavaScript驅動的界面,因此可以設置其樣式以使其在多個瀏覽器中看起來相同。 如果是這種情況,通常您需要使用execute_script執行一些JavaScript,以覆蓋隱藏文件字段的所有CSS,以便它在屏幕上可見,然后調用attach_file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.