如何使用范围和存在为 validates_uniqueness_of 编写测试:true RSpec Rails
How to write a test for validates_uniqueness_of with scope and presence: true RSpec Rails
正在尝试编写测试以验证 :user_id 的唯一性,范围为 :board_id.
型号:
class Membership < ApplicationRecord
belongs_to :board
belongs_to :user
validates_uniqueness_of :user_id, scope: :board_id, presence: true
end
我试着像这样测试它:
it { should validate_uniqueness_of(:user_id) }
it expect.to have_many(:membership).through(:board_id)
感谢您的帮助。
如果使用 shoulda-matchers 可以在两个期望中完成(即使它是一个模型行):
it { should validate_presence_of(:user_id) }
it { should validate_uniqueness_of(:user_id).scoped_to(:board_id) }
正在尝试编写测试以验证 :user_id 的唯一性,范围为 :board_id.
型号:
class Membership < ApplicationRecord
belongs_to :board
belongs_to :user
validates_uniqueness_of :user_id, scope: :board_id, presence: true
end
我试着像这样测试它:
it { should validate_uniqueness_of(:user_id) }
it expect.to have_many(:membership).through(:board_id)
感谢您的帮助。
如果使用 shoulda-matchers 可以在两个期望中完成(即使它是一个模型行):
it { should validate_presence_of(:user_id) }
it { should validate_uniqueness_of(:user_id).scoped_to(:board_id) }