开发者

Test db not rolling back after each run

UPDATE: FIXED!!!!! As I suspected it was a config that was messed up somehow - much hair pulling ensued. For some reason "require 'test_help'" was deleted from test_helper.rb, added it back in and all the tests are in a transaction now.

This smells like a basic config issue but I can't 开发者_高级运维figure out what. Rails 2.3.5, Ruby 1.8.7 patch 173. I am using Shoulda+factory girl and have a test for that creates a couple of users for setup

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 

The tests pass, but problem is these don't seem to be cleared after running - looking at test.log, I see that the transactions commit the inserts! What gives?

  # 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

To work around this I'm just using a teardown block of "Model.all.each(&:destroy)", but I shouldn't have to do that, and it's slow/janky to have to destroy everything I instantiate. The transactions should just rollback...

Table in the test DB is InnoDB:

mysql> select engine from tables where table_name = 'users' and table_schema = 'voltron_test';
+--------+
| engine |
+--------+
| InnoDB |
+--------+

and I'm using transactional fixtures (from test_helper.rb):

class ActiveSupport::TestCase
  use_transactional_fixtures = true
end

Transactions do work (accessing test DB from console):

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)


I've never used the use_transaction_fixtures = true in the test case, but have alwsays had:

class Test::Unit::TestCase
    ....
    self.use_transactional_fixtures = true
    ....
end

in the test/test_helper.rb file that comes with Rails, and never had that problem.


If you are using MyISAM db engine, this is pretty normal, as it doesn't support transactions.


For those who are expecting tests to be wrapped in transactions and yet not using rails, but instead using ActiveSupport and ActiveRecord on their own, you should manually include ActiveRecord::TestFixtures:

ActiveSupport::TestCase.include ActiveRecord::TestFixtures

Otherwise transaction functionality including use_transactional_fixtures/use_transactional_tests will not be defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜