[英]How to write a test for validates_uniqueness_of with scope and presence: true RSpec Rails
Trying to write a test to verify uniqueness of:user_id with scope:board_id.尝试编写一个测试来验证:user_id 与 scope:board_id 的唯一性。
Model: Model:
class Membership < ApplicationRecord
belongs_to :board
belongs_to :user
validates_uniqueness_of :user_id, scope: :board_id, presence: true
end
I try to test it like:我尝试像这样测试它:
it { should validate_uniqueness_of(:user_id) }
it expect.to have_many(:membership).through(:board_id)
Thanks for help.感谢帮助。
If using shoulda-matchers it can be done in two expectations (even though it's a single model line):如果使用 shoulda-matchers 它可以在两个期望中完成(即使它是单个 model 行):
it { should validate_presence_of(:user_id) }
it { should validate_uniqueness_of(:user_id).scoped_to(:board_id) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.