Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 正在验证同一属性测试的存在性和内容类型_Ruby On Rails_Validation_Rspec - Fatal编程技术网

Ruby on rails 正在验证同一属性测试的存在性和内容类型

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

这是我的RubyonRails模型

 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