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 ' 

我搜索了很多地方,但我仍然无法找出背后的原因。 任何的想法?

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

基于此问题的最多投票答案,您必须将文件放在{Rails.root} / spec / fixtures / files下