Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec controller testing with paperclip

So with rails 4.2.0 and the latest rspec I generated a basic test for my controller. I'm just stuck with how to test a paperclip image in the valid_attributes.

From searching around so far I've come up with this (which doesn't work):

let(:valid_attributes) {{name: 'The New Room', description: 'This is the brand new room', size: '250', capacity: '100', price: '650', picture: '#{rails.root}/spec/support/room-controller-valid.jpg', rmcat_id: '1'}}

Is there another way to do this? Or do I need to include a helper to get paperclip to work with RSpec?

The error I'm getting in terminal is:

Failure/Error: room = Room.create! valid_attributes Paperclip::AdapterRegistry::NoHandlerError: No handler found for "\#{rails.root}/spec/support/room-controller-valid.jpg

like image 951
Koxzi Avatar asked Feb 14 '15 02:02

Koxzi


1 Answers

Try setting the Paperclip metadata attributes, instead of providing a real :picture attachment.

...
picture_file_name: 'room-controller-valid.jpg',
...

If you are validating attachment content type or size, set those attributes as well:

...
picture_file_name: 'room-controller-valid.jpg',
picture_content_type: 'image/jpeg',
picture_file_size: 1.megabyte,
...

Of course, this won't pass your file to the controller, so you don't need the file in order to accomplish this. But your model instance should pass validation. From the Paperclip README:

Paperclip will wrap up to four attributes (all prefixed with that attachment's name, so you can have multiple attachments per model if you wish) and give them a friendly front end. These attributes are:

<attachment>_file_name
<attachment>_file_size
<attachment>_content_type
<attachment>_updated_at

By default, only _file_name is required for paperclip to operate. You'll need to add _content_type in case you want to use content type validation.

like image 145
amar47shah Avatar answered Nov 03 '22 09:11

amar47shah