Forum: RSpec Testing file attachment with Paperclip

Posted by Nicholas Wieland (Guest)
on 2008-06-09 14:07
(Received via mailing list)
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.

1)
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
Posted by David Chelimsky (Guest)
on 2008-06-09 14:13
(Received via mailing list)
On Jun 9, 2008, at 7:05 AM, Nicholas Wieland 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
Posted by Nicholas Wieland (Guest)
on 2008-06-09 14:19
(Received via mailing list)
Il giorno 09/giu/08, alle ore 14:11, David Chelimsky ha scritto:

> On Jun 9, 2008, at 7:05 AM, Nicholas Wieland 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
Posted by Rick Denatale (rdenatale)
on 2008-06-09 23:19
(Received via mailing list)
On Mon, Jun 9, 2008 at 8:18 AM, Nicholas Wieland 
<nicholas.wieland@gmail.com>
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/
Posted by Neil Cauldwell (neil)
on 2008-09-06 20:02
Rick Denatale wrote:
> On Mon, Jun 9, 2008 at 8:18 AM, Nicholas Wieland 
> <nicholas.wieland@gmail.com>
> 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!
Posted by Fernando Perez (fernando)
on 2009-01-16 23:42
Nicholas Wieland 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.
> 
> 1)
> 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?
Posted by Nick Hoffman (nickh)
on 2009-01-17 08:14
(Received via mailing list)
On 2009-01-16, at 17:42, Fernando Perez 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]:
http://gist.github.com/48264

I hope that helps!
Nick
Posted by Fernando Perez (fernando)
on 2009-01-17 11:10
> 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]:
> http://gist.github.com/48264
> 
> 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.
Posted by Nick Hoffman (nickh)
on 2009-01-17 17:03
(Received via mailing list)
On 2009-01-17, at 05:10, Fernando Perez 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  =)
Posted by Deven Patel (deven)
on 2010-01-29 05:49
Nicholas Wieland 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.
> 
> 1)
> 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 deven.patel@trinitywebtech.com
Posted by Deven Patel (deven)
on 2010-01-29 05:49
Nicholas Wieland 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.
> 
> 1)
> 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 deven.patel@trinitywebtech.com
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.