Testing file attachment with Paperclip

Does someone have an example on faking a file upload for just ensuring
it gets called, without actually uploading the file to s3.
I thought that stubbing Model.has_attached_file would be enough, but
it doesn’t seem so …

This is what I did:

Video.stub!( :has_attached_file ).with( :name ).and_return( true )

has_attached_file is from paperclip, it gets mixed to the model.

RightAws::AwsError in ‘VideosController should create a valid Video’
AWS access keys are required to operate on S3
/Users/ngw/zooppa-4/app/controllers/videos_controller.rb:6:in `create’
/Users/ngw/zooppa-4/spec/controllers/videos_controller_spec.rb:17:

Maybe I missed something ?

TIA,
ngw

On Jun 9, 2008, at 7:05 AM, Nicholas W. wrote:

Does someone have an example on faking a file upload for just
ensuring it gets called

http://talklikeaduck.denhaven2.com/articles/2008/04/18/rails-integration-test-file-upload-plugin

Cheers,
David

On Mon, Jun 9, 2008 at 8:18 AM, Nicholas W.
[email protected]
wrote:

I haven’t been clear enough, my point is not to fake the file upload
(there’s also fixture_file_upload for that) but to fake the receiver, making
it dumb enough to avoid performing every operation on the file (in my case,
uploading to s3).

I’m pretty sure the problem is that by the time you stub has_attached
file,
which is a class method run when the class is loaded, it’s too late.
Based
on a quick look at the paperclip project site, it looks like what you
want
to stub is the save_attached_files method on the model INSTANCE, which
the
has_attached_file registers as an after_save callback.

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Il giorno 09/giu/08, alle ore 14:11, David C. ha scritto:

On Jun 9, 2008, at 7:05 AM, Nicholas W. wrote:

Does someone have an example on faking a file upload for just
ensuring it gets called

http://talklikeaduck.denhaven2.com/articles/2008/04/18/rails-integration-test-file-upload-plugin

I haven’t been clear enough, my point is not to fake the file upload
(there’s also fixture_file_upload for that) but to fake the receiver,
making it dumb enough to avoid performing every operation on the file
(in my case, uploading to s3).

ngw

Rick Denatale wrote:

On Mon, Jun 9, 2008 at 8:18 AM, Nicholas W.
[email protected]
wrote:

I haven’t been clear enough, my point is not to fake the file upload
(there’s also fixture_file_upload for that) but to fake the receiver, making
it dumb enough to avoid performing every operation on the file (in my case,
uploading to s3).

I’m pretty sure the problem is that by the time you stub has_attached
file,
which is a class method run when the class is loaded, it’s too late.
Based
on a quick look at the paperclip project site, it looks like what you
want
to stub is the save_attached_files method on the model INSTANCE, which
the
has_attached_file registers as an after_save callback.

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Rick, I’m interested finding out how to do this, too (Rspec newbie and
really terrible with mocking & stubbing). Are you suggesting we do
something like this (?);

Paperclip.saved_attached_file.stub!(:avatar).and_return(true)

It’s nice to be able to spec the app actually working with S3 but, my
specs are taking over 3 minutes now and commenting out all the S3
involved specs kinda makes me nervous!

Nicholas W. wrote:

Does someone have an example on faking a file upload for just ensuring
it gets called, without actually uploading the file to s3.
I thought that stubbing Model.has_attached_file would be enough, but
it doesn’t seem so …

This is what I did:

Video.stub!( :has_attached_file ).with( :name ).and_return( true )

has_attached_file is from paperclip, it gets mixed to the model.

RightAws::AwsError in ‘VideosController should create a valid Video’
AWS access keys are required to operate on S3
/Users/ngw/zooppa-4/app/controllers/videos_controller.rb:6:in `create’
/Users/ngw/zooppa-4/spec/controllers/videos_controller_spec.rb:17:

Maybe I missed something ?

TIA,
ngw

I am struggling with Webrat and Paperclip. If I specify:
validates_attachment_presence :image, then AR complains because
there is no image set.

Therefore I decide to remove this validation, and instead use:
validates_presence_of :image_file_name, to trick him. But it still
doesn’t work. When I create a new object, even if I put:
Product.create!(…, :image_file_name => ‘thumb1.png’), AR still
complains and it says that Image file name is missing. There must be
some Paperclip magic which I can’t get around.

The only solution I have found is to remove the validation of the
attachment for passing the tests, but I’d like to keep it.

Anyone ran into this previously?

On 2009-01-16, at 17:42, Fernando P. wrote:

Product.create!(…, :image_file_name => ‘thumb1.png’), AR still
complains and it says that Image file name is missing. There must be
some Paperclip magic which I can’t get around.

The only solution I have found is to remove the validation of the
attachment for passing the tests, but I’d like to keep it.

Anyone ran into this previously?

G’day again Fernando. IIRC, Paperclip’s #save_attached_files is what
takes care of saving the attachment to disk, uploading it to S3, etc.
Here’s a small gist that tests the allowance of JPG files, and ensures
that the file isn’t saved [to disk, for me]:

I hope that helps!
Nick

G’day again Fernando. IIRC, Paperclip’s #save_attached_files is what
takes care of saving the attachment to disk, uploading it to S3, etc.
Here’s a small gist that tests the allowance of JPG files, and ensures
that the file isn’t saved [to disk, for me]:
48264’s gists · GitHub

I hope that helps!
Nick

Hi Nick,

Don’t ask me why, but this morning I tried once again to simply do:
product.image = File.new(‘path_to_image.png’), and it worked perfectly
as expected and passed validations and whatever.

On 2009-01-17, at 05:10, Fernando P. wrote:

Hi Nick,

Don’t ask me why, but this morning I tried once again to simply do:
product.image = File.new(‘path_to_image.png’), and it worked perfectly
as expected and passed validations and whatever.

I’m glad you got it sorted out =)

Nicholas W. wrote:

Does someone have an example on faking a file upload for just ensuring
it gets called, without actually uploading the file to s3.
I thought that stubbing Model.has_attached_file would be enough, but
it doesn’t seem so …

This is what I did:

Video.stub!( :has_attached_file ).with( :name ).and_return( true )

has_attached_file is from paperclip, it gets mixed to the model.

RightAws::AwsError in ‘VideosController should create a valid Video’
AWS access keys are required to operate on S3
/Users/ngw/zooppa-4/app/controllers/videos_controller.rb:6:in `create’
/Users/ngw/zooppa-4/spec/controllers/videos_controller_spec.rb:17:

Maybe I missed something ?

TIA,
ngw

hello dear…

have u got the solution 4 that…then please tell me…

and second one is, from where u get S3 aws_access_key and
aws_secret_access_key and bucket_base_name … or from wher to set
this…

thanks
replay me on [email protected]

Nicholas W. wrote:

Does someone have an example on faking a file upload for just ensuring
it gets called, without actually uploading the file to s3.
I thought that stubbing Model.has_attached_file would be enough, but
it doesn’t seem so …

This is what I did:

Video.stub!( :has_attached_file ).with( :name ).and_return( true )

has_attached_file is from paperclip, it gets mixed to the model.

RightAws::AwsError in ‘VideosController should create a valid Video’
AWS access keys are required to operate on S3
/Users/ngw/zooppa-4/app/controllers/videos_controller.rb:6:in `create’
/Users/ngw/zooppa-4/spec/controllers/videos_controller_spec.rb:17:

Maybe I missed something ?

TIA,
ngw

hello dear…

have u got the solution 4 that…then please tell me…

and second one is, from where u get S3 aws_access_key and
aws_secret_access_key and bucket_base_name … or from wher to set
this…

thanks
replay me on [email protected]