我有跟踪推荐的Referral
模型(即,当社交媒体网站上的用户生成唯一推荐/邀请链接并邀请其他人加入应用时)。
我的模型和规格如下:
模型
# app/models/referral.rb
class Referral < ActiveRecord::Base
# A referral has one user who refers and one user who is being referred. Both are User models.
belongs_to :referrer, class_name: "User", foreign_key: "referrer_id"
belongs_to :referree, class_name: "User", foreign_key: "referree_id"
# Validate presence - don't allow nil
validates :referrer_id, presence: true
validates :referree_id, presence: true
# Referrers can have multiple entries, must be unique to a referree, since they can
# only refer someone once.
# Referees can only BE referred once overall, so must be globally unique
validates :referrer_id, uniqueness: { scope: :referree_id }
validates :referree_id, uniqueness: true
end
规格
# spec/models/referral_spec.rb
require "spec_helper"
RSpec.describe Referral, type: :model do
describe "Associations" do
it { should belong_to(:referrer) }
it { should belong_to(:referree) }
end
describe "Validations" do
it { should validate_presence_of(:referrer_id) }
it { should validate_presence_of(:referree_id) }
it { should_not allow_value(nil).for(:referrer_id) }
it { should_not allow_value(nil).for(:referree_id) }
# These two FAIL
it { should validate_uniqueness_of(:referrer_id).scoped_to(:referree_id) }
it { should validate_uniqueness_of(:referree_id) }
end
end
我的问题是最后两个规范测试总是失败,比如
1) Referral Validations should require case sensitive unique value for referrer_id scoped to referree_id
Failure/Error: it { should validate_uniqueness_of(:referrer_id).scoped_to(:referree_id) }
ActiveRecord::StatementInvalid:
PG::NotNullViolation: ERROR: null value in column "referree_id" violates not-null constraint
DETAIL: Failing row contains (1, 2016-01-13 19:28:15.552112, 2016-01-13 19:28:15.552112, 0, null).
: INSERT INTO "referrals" ("referrer_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"
看起来shoulda
匹配器正在尝试在测试各种值时在其中插入nil
值。使用allow_nil
true
和false
进行游戏并未解决问题。
知道它为什么会在那里磕磕绊绊?
谢谢!
答案 0 :(得分:1)
请参阅this
validate_uniqueness_of匹配器创建一个新实例(如果尚未存在)。这样做时,由于某些其他字段的模型验证,它可能会失败。在您的情况下,在验证referrer_id的唯一性时,referree_id中的值为nil,因此它失败。
解决方案是创建一个实例并为在列中抛出ERROR:null值的属性赋值。
再次,请参阅第一行中的链接以获取示例和详细说明