Can't get uniqueness validation test pass with shoulda matcher Can't get uniqueness validation test pass with shoulda matcher sqlite sqlite

Can't get uniqueness validation test pass with shoulda matcher


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


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) }  endend