fixture_file_upload 有 {file} 不存在错误 [英] fixture_file_upload has {file} does not exist error

查看:21
本文介绍了fixture_file_upload 有 {file} 不存在错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Below is my testing code for uploading file.

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 时,它产生了以下错误

However, when I run rspec spec, it produced me the error below

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

我用谷歌搜索了很多地方,但我仍然无法找出背后的原因.有什么想法吗?

I have googled alot of places but I still couldn't find out what's the reason behind it. Any idea?

推荐答案

Fixture_file_upload 基本上还是可以的.你只需要确保你的 spec_helper.rb 文件中的装置路径没有注释 &正确设置为 spec/fixtures 路径 &包括 ActionDispath::TestProcess:

Fixture_file_upload basically still works. You just need to make sure the fixtures path in your spec_helper.rb file is uncommented & correctly set to the spec/fixtures path & to include 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"

  ...

并且在测试中指定文件的位置,请确保在文件名之前使用/",如下例所示:

And where you are specifying the file in your tests, be sure to precede your file name with '/' like the following example:

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

The absolute path to the file is the base path specified in config.fixture_path plus the relative path specified in fixture_file_upload function call. So, in this example, file.csv must be placed in #{::Rails.root}/spec/fixtures/files/data.csv

这篇关于fixture_file_upload 有 {file} 不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆