Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 验证电子邮件是否存在的测试失败,应达通过,工厂女孩通过_Ruby On Rails_Unit Testing_Rspec_Factory Bot_Shoulda - Fatal编程技术网

Ruby on rails 验证电子邮件是否存在的测试失败,应达通过,工厂女孩通过

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

我正在测试更新时的用户模型不需要电子邮件

与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 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:和。再次感谢。