0
我在使用Rspec 3和carrierwave将图像保存到指定测试目录时遇到了一些问题。我一直在阅读的文档,但我不断面临无法在测试环境中保存载波图像
Failure/Error: @uploader.store!(File.open("#{Rails.root}/spec/fixtures/yp2.jpg"))
NoMethodError:
undefined method `id' for nil:NilClass
# ./spec/rails_helper.rb:73:in `store_dir'
一个TMP的路径是在
/spec/uploads/tmp/
创建,但图像没有保存为预期
/spec/uploads/animal_image/image/12
在以下我的rails_helper.rb
我有
# set test specific store directory
if defined?(CarrierWave)
CarrierWave::Uploader::Base.descendants.each do |klass|
next if klass.anonymous?
klass.class_eval do
def cache_dir
"#{Rails.root}/spec/support/uploads/tmp"
end
def store_dir
"#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
end
end
和我的实际测试
require 'rails_helper'
require 'carrierwave/test/matchers'
describe AnimalImage do
include CarrierWave::Test::Matchers
before(:each) do
AnimalImageUploader.enable_processing = true
@uploader = AnimalImageUploader.new(@animal, :image)
@uploader.store!(File.open("#{Rails.root}/spec/fixtures/yp2.jpg"))
end
after(:each) do
@uploader.remove!
AnimalImageUploader.enable_processing = false
end
context 'Image Versions' do
it 'should scale large_animal_image to 555 x 365 ' do
expect(@uploader.large_animal_image).to have_dimensions(555, 365)
end
end
end
没有人有任何想法,为什么这可能发生?
谢谢
编辑
虽然从我的规格
<AnimalImageUploader:0x00000002dafa28 @model=nil, @mounted_as=:image
如何设置模型做一些调试,我已经捕获@uploader对象?
EDIT 2
因此,采取我设法让图像与图像的所有版本沿着正确的地方节省@rossta提供船上的意见之后,但现在我只是需要找到一个匹配这可以告诉我,每个图像的版本是正确的尺寸
before(:each) do
AnimalImageUploader.enable_processing = true
file = File.open("#{Rails.root}/spec/fixtures/yp2.jpg")
@animal = AnimalImage.create!(image: file)
@uploader = AnimalImageUploader.new(@animal, :image)
@uploader.store!
end
OK越来越近了,如果设置了 '@animal = AnimalImage.create' @animal ='#,@ mounted_as =:image>测试通过但对象中没有任何东西? –
Richlewis
2014-10-09 13:50:37
我不确定你的意思是“对象中没有东西”。我从你的评论中注意到你的'AnimalImage'的'id'是零,这意味着它没有正确保存在数据库中。检查您的验证并确保在创建之前设置所需的值。 – rossta 2014-10-09 14:17:01
谢谢,图像现在保存到正确的位置,看到我的更新有问题,以显示我如何到达那里..只需要找到一个匹配器,现在检查每个图像类型的尺寸 – Richlewis 2014-10-09 15:02:53