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

pbpqsu0x  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(83)

我想为这个验证写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.

任何帮助

e7arh2l6

e7arh2l61#

如果在模型中,

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

相关问题