class Profile < ApplicationRecord
belongs_to :user
validate :first_or_last_name_null
def first_or_last_name_null
if first_name.nil? && last_name.nil?
errors.add(:base, "either first_name or last_name must be present!")
end
end
我不知道我的代码行有什么问题,无法从rspec中获得以下错误。分配rq11验证器:当姓氏存在时,允许个人资料的名字为空失败/错误:期望(Profile.new(:first_name => nil,:last_name =>“ Smith”,:gender =>“ male”))。be_valid预期#<Profile id: nil, gender: "male", birth_year: nil, first_name: nil, last_name: "Smith", user_id: nil, created_at: nil, updated_at: nil>.valid?
返回true,得到false
规格文件具有以下..
context "rq11" do
context "Validators:" do
it "does not allow a User without a username" do
expect(User.new(:username=> "")).to_not be_valid
end
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
it "does not allow a Profile with a gender other than male or female " do
expect(Profile.new(:first_name=>"first", :last_name=>"last", :gender=>"neutral")).to_not be_valid
end
it "does not allow a boy named Sue" do
expect(Profile.new(:first_name=>"Sue", :last_name=>"last", :gender=>"male")).to_not be_valid
end
it "allows a Profile with gender male" do
expect(Profile.new(:first_name=>"first", :last_name=>"last", :gender=>"male")).to be_valid
end
it "allows a Profile with gender female" do
expect(Profile.new(:first_name=>"first", :last_name=>"last", :gender=>"female")).to be_valid
end
end
end
我认为这是无效的,因为user_id
为空。我记得默认情况下,rails会验证关联的存在。将user_id
添加到所有配置文件中,应该没问题
虽然罗曼的答案是正确的,但我想添加更多详细信息和更多选项来解决问题。
您的Profile
belong_to :user
。默认情况下,belongs_to
关联要求关联的对象存在。在这种情况下,必须有一个用户与配置文件相关联,否则该配置文件将无效。
根据您的用例,您可以通过三种选择来解决此问题:
使关联为可选,请阅读有关optional belongs_to
associations in the Rails Guides。如果在您的应用程序上下文中有必要不需要始终存在关联,则这显然只是一个选择。
belongs_to
belongs_to :user, optional: true
禁用内置验证。
您要确保规范中的每个配置文件始终分配有有效的用户。这样的事情可能会起作用:
optional: true
您不仅测试实例是否有效,而且还测试是否存在预期的特定错误
let(:user) { User.find_or_create_by(username: 'test_user') }
it "does not allow a Profile with a null first and last name" do
expect(Profile.new(user: user, first_name: nil, last_name: nil, gender: "male")).to_not be_valid
end