gpt4 book ai didi

ruby-on-rails - 使用 Capybara attach_file 方法测试上传文件时遇到问题

转载 作者:行者123 更新时间:2023-12-04 05:38:31 28 4
gpt4 key购买 nike

我在尝试使用 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:'i@love.oreos', 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: 'i@love.oreos'
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

但是,当我将选择器更改为没有 id '#' 符号的'artist_background_page' 时。 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: "i@love.oreos", 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)>'

在这种情况下,模型已创建,但错误表明由于某种原因无法找到它...
有没有其他方法可以用来在 capybara 中附加文件并测试成功上传?

失败的可能性:
  • 文件上传路径错误
  • 使用方法错误或使用方法无效
  • 服务器出现问题
  • 最佳答案

    这个错误:

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

    显示 capybara 找不到名为 #artist_background_image 的字段.您需要做的是通过名称引用文件上传字段。

    假设你有
    <input id="artist_background_image" name="file_upload" type="file">

    然后您可以引用该字段,如:
    page.attach_file('file_upload', Rails.root + 'spec/Fixtures/Snow.jpg')

    这样, capybara 就会知道要通过哪个字段上传。

    我知道这是一个迟到的回应,但我认为它会帮助其他人面临这个问题。

    关于ruby-on-rails - 使用 Capybara attach_file 方法测试上传文件时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24853066/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com