Define custom validator that permits first_name or last_name to be null but not both
私は働く何かを持っているが、私
def at_least_one_name
if first_name.nil? && last_name.nil?
errors.add(:first_name, "Must contain at least a first or last name")
end
end
これを完全にテストする方法はわかりません。私が上に持っているのは、両方が無ければテストすることだけです。私がエラー配列に:first_name
を追加しているという事実は、すでに何かが間違っていることを私に伝えています。
これはif/else
でしょうか?それらは検証で動作しますか?
編集:私はパスを作成しようとしているテスト:あなたのケースでは
it "does not allow a Profile with a null first and last name" do
expect(Profile.new(:first_name=>nil, :last_name=>nil, :gender=>"male")).to_not be_valid
end
it "allows a Profile with a null first name when last name present" do
expect(Profile.new(:first_name=>nil, :last_name=>"Smith", :gender=>"male")).to be_valid
end
it "allows a Profile with a null last name when first name present" do
expect(Profile.new(:first_name=>"Joe", :last_name=>nil, :gender=>"male")).to be_valid
end
あなたが役に立った場合は、答えをマークする必要があります。 – Nickey