Rails 2.3.2 单元测试在使用普通 ruby​​ 运行时通过,在使用 rake test:units 运行时失败 [英] Rails 2.3.2 unit test passes when run with normal ruby, fails when run with rake test:units

查看:36
本文介绍了Rails 2.3.2 单元测试在使用普通 ruby​​ 运行时通过,在使用 rake test:units 运行时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中创建记录时,当它与 rake test:units 一起运行时,我得到一个ActiveRecord::RecordInvalid".当我使用 ruby​​ ('ruby unit/blah_test.rb') 手动运行单元测试时,我没有收到此错误.我已将问题缩小到我的模型中存在的验证.这是一个包含验证,它实际上是针对不同的模型执行查找以获得有效值.我认为这与夹具未及时加载有关,但我的 test_helper.rb 中确实有夹具:all"(我也尝试在单元测试本身中包含夹具:all".

有人对我如何尝试进一步缩小范围有任何建议吗?

谢谢.

解决方案

当然,您所看到的与缺少固定装置的情况相符.使用 rake test:units 测试数据库模式将被设置(和清除),因此您包含的模型的装置可能不会被填充.通过直接调用,您将在上次离开的状态下使用测试数据库,其中可能包含所包含模型的固定装置.

测试类中是否有另一个对 fixtures 的调用可能导致 fixtures :all 无法启动?

在通过 ruby​​ 运行测试之前,您可以尝试执行 rake db:test:prepare,这意味着您正在一个新的测试数据库上运行.如果没有为您包含的模型加载灯具,这将进一步突出显示.

When creating a record in a unit test, I'm getting an 'ActiveRecord::RecordInvalid' when it's run with rake test:units. I don't get this error when I run the unit test manually with ruby ('ruby unit/blah_test.rb'). I've narrowed the issue down to a validation that exists in my model. It is an inclusion validation which is actually performing a find against a different model to get the valid values. I'm thinking this is related to the fixtures not being loaded in time, but I do have 'fixtures :all' in my test_helper.rb (I've also tried including 'fixtures :all' in the unit test itself.

Does anyone have any suggestions on how I can try to narrow this down even further?

Thanks.

解决方案

Certainly what you are seeing would fit with fixtures being missing. With rake test:units the test database schema will be set up (and cleared) so your included model's fixtures may not be populated. With the direct call you'll be using the test database in the state you last left it which probably does include the fixutres for the included model.

Is there another call to fixtures in the test class which may be causing fixtures :all not to kick in?

You could try doing rake db:test:prepare prior to running your test via ruby which would mean you were running on a fresh test database. This would further highlight if the fixtures aren't being loaded for your included model.

这篇关于Rails 2.3.2 单元测试在使用普通 ruby​​ 运行时通过,在使用 rake test:units 运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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