简体   繁体   English

使用回形针进行RSpec控制器测试

[英]RSpec controller testing with paperclip

So with rails 4.2.0 and the latest rspec I generated a basic test for my controller. 因此,使用rails 4.2.0和最新的rspec,我为我的控制器生成了一个基本测试。 I'm just stuck with how to test a paperclip image in the valid_attributes. 我只是坚持如何在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? 或者我是否需要包含帮助程序才能使回形针与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

Try setting the Paperclip metadata attributes, instead of providing a real :picture attachment. 尝试设置Paperclip元数据属性,而不是提供真实的:picture附件。

...
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 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. Paperclip将包含最多四个属性(所有属性都以该附件的名称为前缀,因此如果您愿意,每个模型可以有多个附件),并为它们提供友好的前端。 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. 默认情况下,回形针只需要_file_name即可运行。 You'll need to add _content_type in case you want to use content type validation. 如果要使用内容类型验证,则需要添加_content_type。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM