我的avatar_parts_spec.rb中有一个单人匹配器,但我无法通过它:

测试:

require 'rails_helper'

RSpec.describe AvatarPart, :type => :model do
  it { should validate_presence_of(:name) }
  it { should validate_presence_of(:type) }
  it { should validate_uniqueness_of(:name).case_insensitive }
  it { should belong_to(:avatar) }
end

模型:
class AvatarPart < ActiveRecord::Base
  attr_accessible :name, :type, :avatar_id

  belongs_to :avatar

  validates_uniqueness_of :name, case_sensitive: false
  validates :name, :type, presence: true, allow_blank: false
end

移民:
class CreateAvatarParts < ActiveRecord::Migration
  def change
    create_table :avatar_parts do |t|
      t.string :name, null: false
      t.string :type, null: false
      t.integer :avatar_id

      t.timestamps
    end
  end
end

错误:
 1) AvatarPart should require unique value for name
     Failure/Error: it { should validate_uniqueness_of(:name).case_insensitive }
     ActiveRecord::StatementInvalid:
       SQLite3::ConstraintException: NOT NULL constraint failed: avatar_parts.type: INSERT INTO "avatar_parts" ("avatar_id", "created_at", "name", "type", "updated_at") VALUES (?, ?, ?, ?, ?)

错误的可能原因是什么?

编辑:
Github仓库:https://github.com/preciz/avatar_parts

最佳答案

该匹配器的The documentation说:



因此,在您的情况下,解决方案将如下所示:

  describe "uniqueness" do
    subject { AvatarPart.new(name: "something", type: "something else") }
    it { should validate_uniqueness_of(:name).case_insensitive }
  end

关于ruby-on-rails - 无法通过比对匹配器获得唯一性验证测试合格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27046691/

10-11 21:38