ruby-on-rails – fixture_file_upload有{file}不存在错误
发布时间:2020-12-16 20:23:05 所属栏目:百科 来源:网络整理
导读:以下是我上传文件的测试代码. 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
以下是我上传文件的测试代码.
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中 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |