1

我使用事务灯具在MINITEST,我的测试运行成功(和通过)当我第一次运行它们:使用Rails Minitest,测试如何通过但在重新测试时失败?

rake test test/models/number_test.rb 
Run options: --seed 31462 
# Running: 
.. 
Finished in 0.271344s, 7.3707 runs/s, 7.3707 assertions/s. 
2 runs, 2 assertions, 0 failures, 0 errors, 0 skips 

然而,当我马上再次运行,他们会失败:

rake test test/models/number_test.rb 
Run options: --seed 22968 
# Running: 
EE 
Finished in 0.058652s, 34.0997 runs/s, 0.0000 assertions/s. 

    1) Error: 
    NumberTest#test_to_param_is_number: 
    ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "calls" violates foreign key constraint "fk_calls_extension_id" 
    DETAIL: Key (extension_id)=(760421015) is not present in table "extensions". 
    : COMMIT 

    2) Error: 
    NumberTest#test_twilio's_API_is_configured_to_come_to_this_number's_URL: 
    ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "calls" violates foreign key constraint "fk_calls_extension_id" 
    DETAIL: Key (extension_id)=(760421015) is not present in table "extensions". 
    : COMMIT 

2 runs, 0 assertions, 0 failures, 2 errors, 0 skips 

我正在使用schema_plus gem将外键添加到我的表中。

由于灯具按字母顺序装载,因此我使用deferrable: :initially_deferred选项,该选项仅在事务结束时执行参照完整性检查,因此所有数据都将在检查之前加载到所有表中。这就是测试第一次运行的原因......但是我不确定为什么它在第二次运行中有什么不同。

运行重新测试时,是不是应该清空所有数据库表并且使用可延期选项重新加载固件?这就像延迟是第一次没有兑现。

为了得到它的工作,我总是必须在运行测试之间运行rake db:reset,这似乎很疯狂。

更新1:如果我注释掉为calls所有的灯具(实际上什么都没有做,在number_test.rb任何测试),一切工作正常…我可以像我喜欢的那样经常重新运行数字测试,并且他们仍然通过。所以,这似乎与延期有关。

回答

1

这是一个真正的参照完整性问题。 numberscalls最终都链接回users

原来,users夹具在测试服务器上不存在。那会做。

0

Rails将尝试禁用数据库中的约束触发器来清除它。你需要让超级用户来完成这个任务。这样做:

我进入sudo -u postgres psql和类型:

ALTER USER yourdbusername SUPERUSER; 
相关问题