Ruby on rails 验证电子邮件是否存在的测试失败,应达通过,工厂女孩通过
我正在测试更新时的用户模型不需要电子邮件 与FactoryGirl:Ruby on rails 验证电子邮件是否存在的测试失败,应达通过,工厂女孩通过,ruby-on-rails,unit-testing,rspec,factory-bot,shoulda,Ruby On Rails,Unit Testing,Rspec,Factory Bot,Shoulda,我正在测试更新时的用户模型不需要电子邮件 与FactoryGirl: u = FactoryGirl.create(:user) u.email = nil expect(u.save).to be_true 考试通过了 与shoulda: should_not validate_presence_of(:email).on(:update) 测试失败,出现错误: Failure/Error: should_not validate_presence_of(:email) Did not
u = FactoryGirl.create(:user)
u.email = nil
expect(u.save).to be_true
考试通过了
与shoulda:
should_not validate_presence_of(:email).on(:update)
测试失败,出现错误:
Failure/Error: should_not validate_presence_of(:email)
Did not expect errors to include "can't be blank" when email is set to nil, got error: can't be blank
有人知道为什么会出现这种差异吗?我想这就是发生的情况
不应
测试使用了User.new
的隐式主题。要在此主题上测试update
,必须先保存它,但由于create
上的验证,保存将导致错误。解决方法是创建/保存有效的用户对象,并在(:update)上显式调用不应验证(…)的存在
请参阅相关的抱歉-我也不得不提前跑步。非常感谢您的帮助和良好的回答。作为参考,我在以下链接中找到了.on(:update):here:和。再次感谢。