[rspec-users] Testing file attachment with Paperclip

860 views
Skip to first unread message

Nicholas Wieland

unread,
Jun 9, 2008, 8:05:47 AM6/9/08
to rspec...@rubyforge.org
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

David Chelimsky

unread,
Jun 9, 2008, 8:11:03 AM6/9/08
to rspec-users
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
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Nicholas Wieland

unread,
Jun 9, 2008, 8:18:36 AM6/9/08
to rspec-users
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

Rick DeNatale

unread,
Jun 9, 2008, 5:18:51 PM6/9/08
to rspec-users

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/

Neil Cauldwell

unread,
Sep 6, 2008, 2:02:11 PM9/6/08
to rspec...@rubyforge.org

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 via http://www.ruby-forum.com/.

Fernando Perez

unread,
Jan 16, 2009, 5:42:20 PM1/16/09
to rspec...@rubyforge.org

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?

Nick Hoffman

unread,
Jan 16, 2009, 11:53:39 PM1/16/09
to rspec...@rubyforge.org

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

Fernando Perez

unread,
Jan 17, 2009, 5:10:59 AM1/17/09
to rspec...@rubyforge.org
> 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.

Nick Hoffman

unread,
Jan 17, 2009, 10:12:09 AM1/17/09
to rspec...@rubyforge.org
On 2009-01-17, at 05:10, Fernando Perez wrote:
>> 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.

I'm glad you got it sorted out =)

Deven Patel

unread,
Jan 28, 2010, 11:49:32 PM1/28/10
to rspec...@rubyforge.org


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...@trinitywebtech.com

Deven Patel

unread,
Jan 28, 2010, 11:49:56 PM1/28/10
to rspec...@rubyforge.org
Reply all
Reply to author
Forward
0 new messages