unit test in rails - model with paperclip
Asked Answered
H

3

34

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?

Horning answered 12/12, 2009 at 23:51 Comment(0)
R
68

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.

Rarefy answered 13/12, 2009 at 8:51 Comment(3)
It gives: NoMethodError: undefined method `attachment='Sankey
@Sankey attachment is generic, like foo; you should replace it with whatever name you used when you added a Paperclip attachment on your model.Koslo
what about adding it in post fixtures?Meany
P
16

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.

Pontifical answered 13/12, 2009 at 9:10 Comment(2)
What is @valid_attributes ?Sankey
It's just a hash of attributes required to create the model, in my test case. You may not need that for your own tests.Pontifical
P
1

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

Pyromancy answered 6/7, 2016 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.