首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >文件有{ fixture_file_upload }不存在错误

文件有{ fixture_file_upload }不存在错误
EN

Stack Overflow用户
提问于 2012-01-26 06:35:17
回答 3查看 14.1K关注 0票数 19

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

代码语言:javascript
复制
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时,它产生了下面的错误

代码语言:javascript
复制
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)>'

我在谷歌上搜索了很多地方,但我仍然找不到背后的原因。有什么想法吗?

EN

回答 3

Stack Overflow用户

发布于 2013-03-06 12:24:29

Fixture_file_upload基本上仍然可以工作。您只需要确保spec_helper.rb文件中的fixtures路径是未注释的,并且正确地设置为spec/fixtures路径并包含ActionDispath::TestProcess

代码语言:javascript
复制
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"

  ...

当您在测试中指定文件时,请确保在文件名前面加上'/‘,如下例所示:

代码语言:javascript
复制
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

票数 29
EN

Stack Overflow用户

发布于 2019-11-13 20:12:05

这就是我用Rails 6RSpecRack::Test::UploadedFile实现的方法

代码语言:javascript
复制
describe 'POST /create' do
  it 'responds with success' do
    post :create, params: {
      company: {
        logo: Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/test-pic.png"),
        name: 'test'
      }
    }

    expect(response).to be_successful
  end
end

DO NOT include ActionDispatch::TestProcess或任何其他代码,除非您确定要包含的内容。

票数 4
EN

Stack Overflow用户

发布于 2012-11-13 08:38:49

根据对this问题投票最多的答案,您必须将该文件放在{Rails.root}/spec/fixtures/files下

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9011425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档