gpt4 book ai didi

ruby-on-rails - 如何测试文件上传模型?

转载 作者:行者123 更新时间:2023-11-28 19:48:04 26 4
gpt4 key购买 nike

我有一个模型,我使用该方法上传图片

在我调用的图像 Controller 中DataFile.save_image_file(参数[:上传])

我的代码data_file.rb

      def self.save_image_file(upload)
file_name = upload['datafile'].original_filename if (upload['datafile'] !='')
file = upload['datafile'].read

file_type = file_name.split('.').last
new_name_file = Time.now.to_i
name_folder = new_name_file
new_file_name_with_type = "#{new_name_file}." + file_type
new_file_name_thumb_with_type = "#{new_name_file}-thumb." + file_type

image_root = "#{RAILS_CAR_IMAGES}"


Dir.mkdir(image_root + "#{name_folder}");
File.open(image_root + "#{name_folder}/" + new_file_name_with_type, "wb") do |f|
f.write(file)
end

[new_name_file, new_file_name_with_type, new_file_name_thumb_with_type]

end

我想在 RSpec 中测试它

data_file_spec.rb

require 'spec_helper'

describe DataFile do
describe "Should save image file" do


before(:each) do
@file = fixture_file_upload('/files/test-bus-1.jpg', 'image/jpg')
end

it "Creating new car name and thumb name" do
@get_array = save_file(@file)
@get_array[:new_name_file].should_not be_nil
end

end
end

但是测试不行

失败/错误:@file = fixture_file_upload('/files/test-bus-1.jpg', 'image/jpg') 没有方法错误: #

的未定义方法“fixture_file_upload”

最佳答案

您需要包含 ActionDispatch::TestProcess。尝试类似的东西:

require 'spec_helper'

describe DataFile do
describe "Should save image file" do
let(:file) do
extend ActionDispatch::TestProcess
fixture_file_upload('/files/test-bus-1.jpg', 'image/jpg')
end

it "Creating new car name and thumb name" do
@get_array = save_file(file)
@get_array[:new_name_file].should_not be_nil
end
end
end

关于ruby-on-rails - 如何测试文件上传模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12121522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com