Reputation: 15384
I am having some issues with Rspec 3 and carrierwave saving an image to a specified test directory. I have been reading the docs but am constantly faced with
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'
A tmp path is created at
/spec/uploads/tmp/
But the image is not saved at the following as expected
/spec/uploads/animal_image/image/12
in my rails_helper.rb
I have
# 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
and my actual test
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
Does anyone have any ideas as to why this could be happening?
Whilst doing some debugging I have captured the @uploader object from my spec
<AnimalImageUploader:0x00000002dafa28 @model=nil, @mounted_as=:image
How do I set the model?
So after taking the comments on board provided by @rossta I have managed to get the images to save in the correct place along with all versions of the image but now I just need to find a matcher that will tell me that the versions of each image are the correct dimensions
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
Upvotes: 0
Views: 1037
Reputation: 11504
In your test, @animal
is the model, but you haven't set it's value to anything. Try creating an animal record or instantiating a stubbed version that respnds to methods like id
:
before(:each) do
AnimalImageUploader.enable_processing = true
@animal = Animal.create!
@uploader = AnimalImageUploader.new(@animal, :image)
@uploader.store!(File.open("#{Rails.root}/spec/fixtures/yp2.jpg"))
end
Upvotes: 1