在 RAILS 中运行特定测试之前运行特定的 rake 测试

发布于 2024-11-05 05:46:33 字数 808 浏览 5 评论 0原文

我正在使用 Rails 提供的内置单元测试功能测试一个应用程序。问题是,一项测试很大程度上依赖于 ids 的计算,我们必须对此进行测试。由于回滚事务时活动记录不会重置 auto_increment,因此我们在这里遇到了一些麻烦。

如果有一种方法可以重置所有数据库表并再次加载装置,就像我刚刚运行了该测试一样,那么事情就可以解决。

首先,我尝试定义一个设置方法来以这种方式重置所有必要的 auto_increment 值:

def setup
  ActiveRecord::Base.connection.execute("ALTER TABLE 'table_name1' AUTO_INCREMENT = 1;") 
  ActiveRecord::Base.connection.execute("ALTER TABLE 'table_name2' AUTO_INCREMENT = 1;")
end

但测试失败并抛出这个丑陋的错误:

ActiveRecord::StatementInvalid: Mysql::错误:保存点 active_record_1 不存在: 回滚到保存点 active_record_1

然后我尝试在设置方法中调用 db:test:prepare 和 db:fixtures:load 但这也失败了。此外,它会大大减慢测试速度,因为设置方法是在文件中的每个测试之前运行的,不是吗?

那么我怎样才能在特定的测试套件或测试文件(不知道如何调用它)之前运行这些rake任务并且仅针对该任务呢?

谢谢! :)

I am testing an app with the built-in unit testing feature provided by rails. The problem is that one test heavily depends on calculations with ids and we must test that. As active record doesn't reset auto_increment when rolling back a transaction we are having some troubles here.

Things could be fixed if there is a way to reset all database tables and load the fixtures again as if I have run just that test.

Firstly I try to define a setup method to reset all the necessary auto_increment values in this way:

def setup
  ActiveRecord::Base.connection.execute("ALTER TABLE 'table_name1' AUTO_INCREMENT = 1;") 
  ActiveRecord::Base.connection.execute("ALTER TABLE 'table_name2' AUTO_INCREMENT = 1;")
end

but the test failed throwing this ugly error:

ActiveRecord::StatementInvalid:
Mysql::Error: SAVEPOINT
active_record_1 does not exist:
ROLLBACK TO SAVEPOINT active_record_1

Then I try to invoke db:test:prepare and db:fixtures:load in the setup method but this failed too. Besides, it will slow up the test a lot since the setup method is run before each test in the file, isn't it?

So how can I run those rake task just before a specific test suite or test file (don't know how to call it) and only for that one?

Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-11-12 05:46:33

您应该能够删除测试数据库,然后运行 ​​rake db:create RAILS_ENV=test 来从测试中重新创建数据库。然而,如果您在测试过程中这样做,您还需要使用有效数据重新播种数据库,如果您想采取这种方式,这可能会是一件很痛苦的事情。

您还应该能够使用迁移中的“drop_table :table_name”内容来执行此操作。

但是,您的测试不应该以这种方式编写,您的数据库也不应该以这种方式构建,它们依赖于特定的数据库 ID。您的测试也不应该仅仅为了重置 AUTO_INCRMENT 值而重新创建整个表。这几乎肯定表明您没有按照设计的方式使用测试套件工具。但是,我也认识到这不是一个完美的世界,我希望这是某种不寻常的边缘情况。 :S

You should be able to delete your test database, and then run rake db:create RAILS_ENV=test to recreate the database from within the test. If you do that in the middle of a test, however, you'll also need to re-seed the database with valid data, which can be a major pain in the ass, if that's the route you want to take.

You should also be able to use the "drop_table :table_name" stuff from migrations to do this.

HOWEVER, your tests shouldn't be written in such a way, nor should your DB be structured in such a way, they they rely on specific DB id's. Nor should your tests need to recreate whole tables just to reset the AUTO_INCREMENT value. This is almost certainly a sign that you're not using your test suite tools the way they are designed to be used. But, I also recognize that it's not a perfect world, and I'm hoping this is some kind of unusual edge case. :S

情绪失控 2024-11-12 05:46:33

您可以使用 database_cleaner gem。然后在你的database_cleaner.rb上,你可以指定:pre_count:reset_ids来重置你的id。我建议您的测试环境也使用单独的数据库。

You can use a database_cleaner gem. then on your database_cleaner.rb, you can specify :pre_count and :reset_ids to reset you ids. I recommend to use a separate database for your test environment as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文