I'm trying to write a test for a model with a picture, using paperclip. I'm using the test framework default, no shoulda or rspec. In this context, how should I test it? Should I really upload a file? How should I add a file to the fixture?
Adding file to a model is dead simple. For example:
@post = Post.new
@post.attachment = File.new("test/fixtures/sample_file.png")
# Replace attachment= with the name of your paperclip attachment
In that case you should put the file into your test/fixtures
dir.
I usually make a little helper in my test_helper.rb
def sample_file(filename = "sample_file.png")
File.new("test/fixtures/#{filename}")
end
Then
@post.attachment = sample_file("filename.txt")
If you use something like Factory Girl instead of fixtures this becomes even easier.
attachment
is generic, like foo
; you should replace it with whatever name you used when you added a Paperclip attachment on your model. –
Koslo This is in Rspec, but can be easily switched over
before do # setup
@file = File.new(File.join(RAILS_ROOT, "/spec/fixtures/paperclip", "photo.jpg"), 'rb')
@model = Model.create!(@valid_attributes.merge(:photo => @file))
end
it "should receive photo_file_name from :photo" do # def .... || should ....
@model.photo_file_name.should == "photo.jpg"
# assert_equal "photo.jpg", @model.photo_file_name
end
Since Paperclip is pretty well tested, I usually don't focus too much on the act of "uploading", unless i'm doing something out of the ordinary. But I will try to focus more on ensuring that the configurations of the attachment, in relation to the model it belongs, meet my needs.
it "should have an attachment :path of :rails_root/path/:basename.:extension" do
Model.attachment_definitions[:photo][:path].should == ":rails_root/path/:basename.:extension"
# assert_equal ":rails_root/path/:basename.:extension", Model.attachment_definitions[:photo][:path]
end
All the goodies can be found in Model.attachment_definitions
.
I use FactoryGirl, setup the Model.
#photos.rb
FactoryGirl.define do
factory :photo do
image File.new(File.join(Rails.root, 'spec', 'fixtures', 'files', 'testimg1.jpg'))
description "testimg1 description"
end # factory :photo
end
then
# in spec
before(:each) { @user = FactoryGirl.create(:user, :with_photo) }
In the paperclip attachment specify where its going to be saved i.e.
...
the_path= "/:user_id/:basename.:extension"
if Rails.env.test?
the_path= ":rails_root/tmp/" + the_path
end
has_attached_file :image, :default_url => ActionController::Base.helpers.asset_path('missing.png'),
:path => the_path, :url => ':s3_domain_url'
Paperclip.interpolates :user_id do |attachment, style|
attachment.instance.user_id.to_s
end
...
then test both the attachment_definitions (as suggested by kwon) and the Dir.glob to check the file is saved
it "saves in path of user.id/filename" do
expect(Dir.glob(File.join(Rails.root, 'tmp', @user.id.to_s, @user.photo.image.instance.image_file_name)).empty?).to be(false)
end
this way i'm sure its carrying out the correct directy/path create etc
© 2022 - 2024 — McMap. All rights reserved.