Fixture_file_upload 有 {file} 不存在错误

2024-05-04

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

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

然而,当我跑步时规格规格,它给我带来了下面的错误

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Fixture_file_upload 有 {file} 不存在错误 的相关文章

随机推荐