[rspec-users] Testing file attachment with Paperclip

Nick Hoffman nick at deadorange.com
Fri Jan 16 23:53:39 EST 2009


On 2009-01-16, at 17:42, Fernando Perez wrote:
> 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?

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


More information about the rspec-users mailing list