【发布时间】:2010-09-18 08:35:10
【问题描述】:
更新:已修复!!!!!! 正如我怀疑的那样,这是一个以某种方式搞砸的配置 - 随之而来的是大量的头发拉扯。出于某种原因,“require 'test_help'”已从 test_helper.rb 中删除,将其重新添加,所有测试现在都在事务中。
这听起来像是一个基本的配置问题,但我不知道是什么。 Rails 2.3.5,Ruby 1.8.7 补丁 173。我正在使用 Shoulda+factory girl 并进行了测试,为设置创建了几个用户
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 'molly.perkins.test@gmail.com', @user.email
end
should "not populate email if already present" do
@user = Factory.create(:amanda_levy)
@user.get_email(@facebook_session)
assert_equal 'amandalevy06@gmail.com', @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(&:destroy)”的拆解块,但我不应该这样做,而且必须销毁我的所有东西很慢/很麻烦实例化。事务应该只是回滚......
测试数据库中的表是 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
事务确实有效(从控制台访问测试数据库):
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?
-
是的,我是——尽管我只是对其进行了调整,无论是否指定主键都没有区别。虽然我理解它的方式,但如果事务回滚,这应该没关系?
-
很奇怪。没有其他地方将 use_transactional_fixtures 设置为 false,在您的测试之前包含或执行?不要忘记测试是按字母顺序完成的,而不是它们的书写顺序。
-
Stephen,我发现无论出于何种原因,我在 test_helper 中都没有“require 'test_help'”。感谢您的回复并让我的大脑正常工作,我将您的答案标记为正确的答案,以便您获得赏金:)
标签: ruby-on-rails