简体   繁体   English

如何在 Rails Rspec 中使用 Scope 编写唯一性测试用例

[英]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我想为此验证编写 RSpec 测试,但它总是给我错误

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任何帮助

If in model you have如果在 model 你有

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM