Ruby on rails 正在验证同一属性测试的存在性和内容类型
这是我的RubyonRails模型Ruby on rails 正在验证同一属性测试的存在性和内容类型,ruby-on-rails,validation,rspec,Ruby On Rails,Validation,Rspec,这是我的RubyonRails模型 class Item < ApplicationRecord has_attached_file :image, :styles => { :medium => "400x400>", :thumb => "100x100>", :square => "300x300#" }, default_url: "/images/:style/missing.png" validates_pres
class Item < ApplicationRecord
has_attached_file :image,
:styles => { :medium => "400x400>", :thumb => "100x100>", :square => "300x300#" }, default_url: "/images/:style/missing.png"
validates_presence_of(:description, :color, :category, :size, :image)
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
belongs_to :user
end
根据自述文件,您可以使用单个验证程序语句进行多个验证。大概是这样的:
class Item < ApplicationRecord
has_attached_file :image,
:styles => { :medium => "400x400>", :thumb => "100x100>", :square => "300x300#" }, default_url: "/images/:style/missing.png"
validates_presence_of(:description, :color, :category, :size)
validates_attachment :image,
content_type: { :content_type => /\Aimage\/.*\Z/ }
presence: true
belongs_to :user
end
我想出来了。结果很简单
it "is not valid without an image" do
expect{item[:image] = nil}.to raise_error(NameError)
end
仍然不知道如何测试它
it "is not valid without an image" do
expect{item[:image] = nil}.to raise_error(NameError)
end