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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – fixture_file_upload有{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规范时,它产生了以下错误

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

我已经google了很多地方,但我仍然找不到它背后的原因.任何想法?

解决方法

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相关文章