gpt4 book ai didi

ruby-on-rails - fixture_file_upload具有{file}不存在错误

转载 作者:行者123 更新时间:2023-12-03 13:57:40 24 4
gpt4 key购买 nike

以下是我上传文件的测试代码。

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


但是,当我运行rspec spec时,它产生了以下错误

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)>'


我已经在很多地方搜索过,但是我仍然找不到背后的原因。任何想法?

最佳答案

Fixture_file_upload基本上仍然可以使用。您只需要确保spec_helper.rb文件中的装置路径未注释且正确设置为spec/fixtures路径并包含ActionDispath::TestProcess

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"

...


并且在要在测试中指定文件的位置,请确保在文件名前加上“ /”,如以下示例所示:

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


文件的绝对路径是 config.fixture_path中指定的基本路径加上 fixture_file_upload函数调用中指定的相对路径。因此,在此示例中,必须将 file.csv放在 #{::Rails.root}/spec/fixtures/files/data.csv

关于ruby-on-rails - fixture_file_upload具有{file}不存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011425/

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