🖥

Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher::Existing

2023/09/01に公開

Answer

Create some record before test case

require "rails_helper"

RSpec.describe User, type: :model do
  describe "validations" do
    # Some way create one record
    before do
      User.create(name: "Alice #{rand(99999999)}")
      # FactoryBot.create :user
    end

    it { should validate_uniqueness_of(:name) }
  end
end

Error

     Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher::ExistingRecordInvalid:
       validate_uniqueness_of works by matching a new record against an
       existing record. If there is no existing record, it will create one
       using the record you provide.

       While doing this, the following error was raised:

...

       The best way to fix this is to provide the matcher with a record where
       any required attributes are filled in with valid values beforehand.

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/3071

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2020-04-14

Discussion