Rails Rspec でスコープを使用して一意性のテスト ケースを作成する方法
概要
この検証のために RSpec テストを書きたいのですが、常にエラーが発生します
validates :post_id, uniqueness: { scope: :user_id }
このソリューションを使用していますが、機能しません
it { is_expected.to validate_uniqueness_of(:post_id).scoped_to(:user_id, :created_at) }
エラー
Like is expected to validate that :post_id is case-sensitively unique within the scope of :user_id and :created_at
Failure/Error: it { is_expected.to validate_uniqueness_of(:post_id).scoped_to(:user_id, :created_at) }
Expected Like to validate that :post_id is case-sensitively unique
within the scope of :user_id and :created_at, but this could not be
proved.
Expected the validation to be scoped to :user_id and :created_at, but
it was scoped to :user_id instead.
何か助けがあれば
解決策
モデルにある場合
validates :post_id, uniqueness: { scope: :user_id }
それからスペックが必要です
it { is_expected.to validate_uniqueness_of(:post_id).scoped_to(:user_id) }
または
it { should validate_uniqueness_of(:post_id).scoped_to(:user_id) }