不一致的 Rails 测试结果 [英] Inconsistent Rails Test Results

查看:44
本文介绍了不一致的 Rails 测试结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几次我运行 rspec spec 时,我收到失败,如果我再次运行它,此后它通过了.为什么会发生这种情况,我该如何解决?

The first couple times I run rspec spec, I receive failures and if I run it again and thereafter, it passes. Why is this happening and how do I fix it?

这似乎与唯一性代码有关,以防止添加名称已存在的记录.以下是第一次始终失败的测试:

This appears to be related to uniqueness code to prevent adding a record with a name that has already been. Below is the test that consistently fails the first time:

it { should validate_uniqueness_of(:name).case_insensitive.with_message(/has already been taken. Please use a different name./) }

可以在此处找到完整的存储库:https://github.com/melissajstudent/koth

The full repo can be found here: https://github.com/melissajstudent/koth

推荐答案

您认为唯一性是正在发生的事情的一个很好的指标,这是正确的.您的测试数据库可能有一些您上次运行测试时的剩余记录,并且正在尝试使用相同的工厂创建记录.

You are right in thinking that the uniquess is a good indicator of what’s happening. Likely your test database has some remaining records from the last time you ran the tests and is trying to creat records with the same factories.

为此,有几种方法可以在测试套件运行之间清理数据库.有时,在运行测试套件之前运行 bundle exec rake db:test:prepare 会很有帮助.在其他情况下,您可以在整个运行过程中使用数据库清理器 gem 实现一些更强大的数据库清理:https://github.com/DatabaseCleaner/database_cleaner

To that end, there are a few ways to clean your database in between runs of your test suites. Sometimes it’s helpful just to run bundle exec rake db:test:prepare Before running your test suite. In other cases you can implement some more robust database cleaning throughout your run with the database cleaner gem: https://github.com/DatabaseCleaner/database_cleaner

这篇关于不一致的 Rails 测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆