簡體   English   中英

如何在 Rails Rspec 中使用 Scope 編寫唯一性測試用例

[英]How to write Test case for uniqueness with Scope in 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.

任何幫助

如果在 model 你有

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM