gpt4 book ai didi

ruby-on-rails - rspec + 载波 : Images are not created

转载 作者:行者123 更新时间:2023-12-04 05:08:26 28 4
gpt4 key购买 nike

我有这个规范:

post :create, :room_id => @room.id, :image => Rack::Test::UploadedFile.new(path, 'text/jpg')
response.should redirect_to admin_room_path(@room)

@room.reload.pictures.count.should == 1
pic = @room.pictures.first

File.open(pic.image.url).should == File.open(path)
# Not working

它失败并出现以下错误:
1) Admin::PicturesController should fail to upload a new picture
Failure/Error: File.open(pic.image.url).should == File.open(path)
Errno::ENOENT:
No such file or directory - /uploads/picture/image/5134d0fa93ff007fcd000016/image1.jpg
# ./spec/controllers/admin/pictures_controller_spec.rb:26:in `initialize'
# ./spec/controllers/admin/pictures_controller_spec.rb:26:in `open'
# ./spec/controllers/admin/pictures_controller_spec.rb:26:in `block (2 levels) in <top (required)>'

文件未找到,但是目录 /uploads/picture/image/5134d0fa93ff007fcd000016/被创建。

除文件外的所有内容都是正确的。

我的初始化程序:
if Rails.env.test? or Rails.env.cucumber?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = true
end
end

我错过了什么吗?

最佳答案

pic.image.url是没有主机的 URL 路径。

尝试:

File.open(File.join(Rails.root, "public", pic.image.url)).should == File.open(path)

或者,我的偏好,让carrierwave处理它:
File.open(pic.image.path).should == File.open(path)

或比较内容:
pic.image.read.should == File.open(path).read

关于ruby-on-rails - rspec + 载波 : Images are not created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15206795/

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