UPDATE:FIXED !!!!! 正如我怀疑这是一个配置,不知何故搞砸了 - 随之而来的是大量的头发拉动。由于某种原因,“test_help'”test'被从test_helper.rb中删除,重新加入并且所有测试都在事务中。测试db在每次运行后都不回滚
这听起来像一个基本的配置问题,但我不知道什么。 Rails的2.3.5,红宝石1.8.7补丁173.我使用早该+工厂女孩,有专门的测试设置
class UserTest < ActiveSupport::TestCase
use_transactional_fixtures = true
context "getting a user's email" do
setup do
... stubs ...
end
should "populate email field if not present" do
@user = Factory.create(:molly_perkins)
@user.get_email(@facebook_session)
assert_equal '[email protected]', @user.email
end
should "not populate email if already present" do
@user = Factory.create(:amanda_levy)
@user.get_email(@facebook_session)
assert_equal '[email protected]', @user.email
end
end
end
测试通过创建几个用户,但问题是这些不运行后似乎被清除 - 看着test.log,我看到这些事务提交插入!是什么赋予了?
# First test
User Create (0.3ms) INSERT INTO `users` ...
SQL (0.4ms) COMMIT
# Second test
SQL (0.1ms) BEGIN
User Create (0.3ms) INSERT INTO `users` ....
SQL (0.4ms) COMMIT
SQL (0.1ms) BEGIN
User Update (0.4ms) UPDATE `users` ....
SQL (0.4ms) COMMIT
若要解决此我只是用的拆解块“Model.all.each(&:销毁)”,但我不应该这样做,而且它的速度慢/ janky有摧毁我实例化的一切。该交易应该只是回滚...
表测试DB是InnoDB的:
mysql> select engine from tables where table_name = 'users' and table_schema = 'voltron_test';
+--------+
| engine |
+--------+
| InnoDB |
+--------+
和我使用的事务夹具(从test_helper.rb中):
class ActiveSupport::TestCase
use_transactional_fixtures = true
end
做交易工作(从控制台访问测试DB):
mysql> select * from users;
Empty set (0.00 sec)
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `users` ...
Query OK, 1 row affected (0.00 sec)
mysql> select * from users;
...
1 row in set (0.00 sec)
mysql> ROLLBACK;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from users;
Empty set (0.00 sec)
您是否在工厂中指定了用户ID? – 2010-09-18 12:34:03
是的,我是 - 虽然我只是调整它,并且不管主键是否被指定都没有区别。虽然我理解它的方式,如果事务回滚,这应该不重要? – ambertch 2010-09-18 18:23:00
很奇怪。 use_transactional_fixtures设置为false时没有其他地方,在测试之前包含或执行了use_transactional_fixtures?不要忘记,测试是按字母顺序完成的,而不是按照它们的顺序来完成的。 – stephenr 2010-09-20 19:27:26