RSpec 用回形针测试控制器
RSpec controller testing with paperclip
所以使用 rails 4.2.0 和最新的 rspec 我为我的控制器生成了一个基本测试。我只是坚持如何在 valid_attributes.
中测试回形针图像
到目前为止,通过四处搜索,我想到了这个(不起作用):
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'}}
还有其他方法吗?或者我是否需要包括一个助手来让回形针与 RSpec 一起工作?
我在终端中遇到的错误是:
Failure/Error: room = Room.create! valid_attributes
Paperclip::AdapterRegistry::NoHandlerError:
No handler found for "\#{rails.root}/spec/support/room-controller-valid.jpg
尝试设置 Paperclip 元数据属性,而不是提供真正的 :picture
附件。
...
picture_file_name: 'room-controller-valid.jpg',
...
如果您要验证附件内容类型或大小,请同时设置这些属性:
...
picture_file_name: 'room-controller-valid.jpg',
picture_content_type: 'image/jpeg',
picture_file_size: 1.megabyte,
...
当然,这不会将您的文件传递给控制器,因此您不需要该文件即可完成此操作。但是您的模型实例应该通过验证。来自回形针 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.
所以使用 rails 4.2.0 和最新的 rspec 我为我的控制器生成了一个基本测试。我只是坚持如何在 valid_attributes.
中测试回形针图像到目前为止,通过四处搜索,我想到了这个(不起作用):
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'}}
还有其他方法吗?或者我是否需要包括一个助手来让回形针与 RSpec 一起工作?
我在终端中遇到的错误是:
Failure/Error: room = Room.create! valid_attributes
Paperclip::AdapterRegistry::NoHandlerError:
No handler found for "\#{rails.root}/spec/support/room-controller-valid.jpg
尝试设置 Paperclip 元数据属性,而不是提供真正的 :picture
附件。
...
picture_file_name: 'room-controller-valid.jpg',
...
如果您要验证附件内容类型或大小,请同时设置这些属性:
...
picture_file_name: 'room-controller-valid.jpg',
picture_content_type: 'image/jpeg',
picture_file_size: 1.megabyte,
...
当然,这不会将您的文件传递给控制器,因此您不需要该文件即可完成此操作。但是您的模型实例应该通过验证。来自回形针 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.