Can't get uniqueness validation test pass with shoulda matcher
Asked Answered
O

2

26

I have a shoulda matcher in my avatar_parts_spec.rb and I can't get it pass:

Test:

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

Model:

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

Migration:

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

Error:

 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 (?, ?, ?, ?, ?)

What could be the cause of the error?

Edit: Github repo: https://github.com/preciz/avatar_parts

Olathe answered 20/11, 2014 at 18:18 Comment(0)
H
58

The documentation for that matcher says:

This matcher works a bit differently than other matchers. As noted before, it will create an instance of your model if one doesn't already exist. Sometimes this step fails, especially if you have database-level restrictions on any attributes other than the one which is unique. In this case, the solution is to populate these attributes with before you call validate_uniqueness_of.

So in your case the solution would be something like:

  describe "uniqueness" do
    subject { AvatarPart.new(name: "something", type: "something else") }
    it { should validate_uniqueness_of(:name).case_insensitive }
  end
Heartfelt answered 20/11, 2014 at 20:50 Comment(3)
Sometimes this seems to be needed, sometimes not. Strange.Thorstein
Why is case_insensitive required here ?Kylakylah
@VishalVijay That was in the original question, it's not a particularly substantive part of the answer.Heartfelt
G
5

In addition to above, a pattern I've used that resolves it:

RSpec.describe AvatarPart, :type => :model
  describe 'validations' do
    let!(:avatar_part) { create(:avatar_part) }

    it { should validate_uniqueness_of(:some_attribute) }
    it { should validate_uniqueness_of(:other_attribute) }
  end
end
Goodall answered 21/12, 2015 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.