提交 cd50e952 编写于 作者: G Guilherme Mansur

Fix test flakyness due to `test_truncate_tables`

`Truncate Tables posts` will also reset the `AUTOINCREMENT` this causes
a situation where if a test suite that uses the `taggings` fixtures runs
and subsequently the `test_truncate_tables` run, newly created posts
would reference the `tagging` in the database. This commit resest the
state of the posts table after the `connection.truncate` call in the
`test_truncate_tables`, as well as all other tests that call `trucate`
This ensures the associations and db state remain consistent after the
tests.

Fixes: https://github.com/rails/rails/issues/35941
上级 713f6243
......@@ -490,6 +490,8 @@ def test_truncate
@connection.truncate("posts")
assert_equal 0, Post.count
ensure
reset_fixtures('posts')
end
def test_truncate_with_query_cache
......@@ -501,6 +503,7 @@ def test_truncate_with_query_cache
assert_equal 0, Post.count
ensure
reset_fixtures('posts')
@connection.disable_query_cache!
end
......@@ -514,6 +517,8 @@ def test_truncate_tables
assert_equal 0, Post.count
assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count
ensure
reset_fixtures('posts', 'authors', 'author_addresses')
end
def test_truncate_tables_with_query_cache
......@@ -529,6 +534,7 @@ def test_truncate_tables_with_query_cache
assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count
ensure
reset_fixtures('posts', 'authors', 'author_addresses')
@connection.disable_query_cache!
end
......@@ -551,6 +557,16 @@ def test_reset_table_with_non_integer_pk
assert_nothing_raised { sub.save! }
end
end
private
def reset_fixtures(*fixture_names)
ActiveRecord::FixtureSet.reset_cache
fixture_names.each do |fixture_name|
ActiveRecord::FixtureSet.create_fixtures(FIXTURES_ROOT, fixture_name)
end
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册