下面是我上传文件的测试代码。
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时,它产生了下面的错误
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)>'我在谷歌上搜索了很多地方,但我仍然找不到背后的原因。有什么想法吗?
发布于 2013-03-06 12:24:29
Fixture_file_upload基本上仍然可以工作。您只需要确保spec_helper.rb文件中的fixtures路径是未注释的,并且正确地设置为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中
发布于 2019-11-13 20:12:05
这就是我用Rails 6、RSpec和Rack::Test::UploadedFile实现的方法
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
endDO NOT include ActionDispatch::TestProcess或任何其他代码,除非您确定要包含的内容。
发布于 2012-11-13 08:38:49
根据对this问题投票最多的答案,您必须将该文件放在{Rails.root}/spec/fixtures/files下
https://stackoverflow.com/questions/9011425
复制相似问题