Rails 2.3.5 表在测试运行结束时而不是开始时由固定装置填充

发布于 2024-08-31 12:53:37 字数 401 浏览 4 评论 0原文

我从包含架构但表中没有数据的测试数据库开始。我运行了一个测试,这样

cd test/
ruby unit/directive_test.rb

我就失败了,表明代码在数据表中没有找到数据。但是,我在运行该测试后查看表,数据现在位于表中。事实上,如果我立即再次运行测试,就不会出现任何失败。

因此,该夹具加载到表中的时间似乎太晚了,我的模块之一无法找到它。

  1. 夹具什么时候加载? app/model/*.rb 文件执行之后还是之前?
  2. 如果是在模型执行之后,有没有办法延迟加载?

运行 rake test:units 时也存在此问题,因为该任务完成后会清除测试数据。

I start with a test database containing the schema but with no data in the tables. I run a test like so

cd test/
ruby unit/directive_test.rb

I get failures indicating that the code found no data in the data tables. However, I look at the tables after running that test and the data is now in the the table. In fact, if I immediately run the test again I get no failures.

So it appears that the fixture is being loaded into the table too late for one of my modules to find it.

  1. When are the fixtures loaded? After or before the app/model/*.rb files are executed?
  2. If it is after the models are executed is there a way to delay the loading?

This issue is also relevant when running rake test:units since that task clears the test data after it finished.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-09-07 12:53:37

首先请参阅此线程并看看它是否可以帮助您。

如果您运行 Rail 任务 rake test:units ,它肯定会在运行代码之前加载所有装置。如果您只运行测试,并且您的单元测试没有引用 test_help.rb ,那么它可能不会加载固定装置。您应该尝试通过 rake 任务运行它。

我给你的另一个提示是你忘记了固定装置并使用工厂(这里我推荐factory_girl)。需要一些时间来适应,但这是值得的。夹具太难管理、更新等。

还有另一篇文章 很少解释工厂背后的概念。

first of all see this thread and see if it can help you.

if you run the rail task rake test:units it will for sure load all fixtures before you run your code. if you are running just the test, and your unit test has no reference to the test_help.rb probably it is not loading the fixtures. You should try to run it through the rake tasks.

Another tip that i give you is that you forget the fixtures and use factories (here i recommend factory_girl). It takes sometime to get used, but it worth. Fixtures are too hard to manage, update and etc.

there is another post explaing little about the concept behind factories.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文