Hammad Abdullah
Hammad Abdullah

Reputation: 31

How to write Test case for uniqueness with Scope in Rails Rspec

I want to write RSpec test for this validation but it always give me error

validates :post_id, uniqueness: { scope: :user_id }

I am using this solution but it is not working

it { is_expected.to validate_uniqueness_of(:post_id).scoped_to(:user_id, :created_at) }

Error

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.

Any help

Upvotes: 0

Views: 109

Answers (1)

mechnicov
mechnicov

Reputation: 15248

If in model you have

validates :post_id, uniqueness: { scope: :user_id }

then you need in specs

it { is_expected.to validate_uniqueness_of(:post_id).scoped_to(:user_id) }

or

it { should validate_uniqueness_of(:post_id).scoped_to(:user_id) }

Upvotes: 1

Related Questions