清除 Rails 中单元测试和功能测试之间的测试数据库 (factory_girl)

发布于 2024-07-29 04:07:34 字数 460 浏览 9 评论 0原文

最近,我从 Fixtures 切换到 Factory_girl 来测试我的 Ruby on Rails 应用程序。 如果我运行 rake test:units 来运行 /units 目录中的测试,它们都会完美运行。 如果我使用 rake test:functions 运行功能测试(在我的 /function 目录中),情况也是如此。

但是,如果我只是运行 rake 测试,同时运行单元测试和功能测试,则我的验证在第二组测试(在本例中为功能测试)上失败,并显示消息“验证失败:名称已被使用”。

我相信这是由于功能测试创建的对象与单元测试中创建的对象具有相同的参数引起的——这使我相信测试数据库在单元测试和功能测试之间没有被清除。

我使用factory_girl的排序来为对象提供唯一的属性,这意味着factory_girl在测试之间被重置,而数据库则不然。 我可以做什么来解决这个问题? 有没有办法清除我的两个测试包之间的数据库?

Recently I switched from fixtures to factory_girl to test my Ruby on Rails application. If I run rake test:units, to run the tests in my /units directory, they all run perfectly. The same is true if I run my functional tests (in my /functional directory) with rake test:functionals.

However, if I simply run rake test, to run both my unit and functional tests together, my validation fails on the second group of tests (functional, in this case), with the message "Validation failed: Name has already been taken."

I believe this is caused by the functional tests creating objects with the same parameters as the objects that were created in the unit tests -- leading me to believe that the test database isn't cleared in between the unit and functional tests.

I use factory_girl's sequencing to have unique attributes for objects, which means that factory_girl is being reset between tests, while the database is not. What can I do to solve this problem? Is there a way to clear the database between my two test packages?

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

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

发布评论

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

评论(6

不知在何时 2024-08-05 04:07:34

清除(重建)测试数据库的命令行解决方案:

rake db:test:prepare

A command line solution to clear (reconstruct) test database:

rake db:test:prepare
弥繁 2024-08-05 04:07:34

尝试在您的 test/test_helper.rb 中编写此内容,

eval IO.read(File.expand_path(File.dirname(__FILE__) + "/../Rakefile"))
class Test::Unit::TestCase
 ....
 #db:test:prepare won't work, don't know why,
 #as DROP DATABASE won't execute (me on PostgreSQL).
 #even if I write,
 #ActiveRecord::Base.connection.disconnect!
 Rake::Task["db:reset"].invoke
end

这不是推荐的解决方案。 它会使测试变慢,但它确实有效。

Try writing this in your test/test_helper.rb

eval IO.read(File.expand_path(File.dirname(__FILE__) + "/../Rakefile"))
class Test::Unit::TestCase
 ....
 #db:test:prepare won't work, don't know why,
 #as DROP DATABASE won't execute (me on PostgreSQL).
 #even if I write,
 #ActiveRecord::Base.connection.disconnect!
 Rake::Task["db:reset"].invoke
end

It's not a recommended solution. It makes tests slower, but it works.

满天都是小星星 2024-08-05 04:07:34

名为“override_rake_task”的 Rails 插件可用于覆盖在 Rails gem 中定义的 Rake 任务“test”。 这是一个非常简单的任务,它依次执行另外 3 个任务:test:unitstest:functionstest:integration。 您可以在执行 test:functions 之前执行“db:test:purge”任务来清除测试数据库。

显然,如果您没有使用此插件,并且您在 Rails 应用程序中定义了具有相同名称的任务,则 rake 将执行这两个任务:默认任务和您的任务。

A rails plugin called "override_rake_task" could be used to override Rake task "test" which is defined inside if Rails gem. This is a very simple task that executes 3 other tasks one after another: test:units, test:functionals and test:integration. You could include the execution of "db:test:purge" task to clear the test DB before executing test:functionals.

Apparently if you are not using this plugin and if you define a task in your rails application with the same name, rake would execute both tasks: the default and yours.

三寸金莲 2024-08-05 04:07:34

上述解决方案对我不起作用。 如果您尝试访问外部数据库,运行单元测试可能会出现一些奇怪的错误。 由于某种原因,它们在运行测试后不会被清除,因此您必须在运行单元测试后运行 rake db:test:purge 。 将其放入您的 Rakefile 中,它应该可以修复它。

Rake::Task["db:test:prepare"].enhance do
  Rake::Task["db:test:purge"].invoke
end

The above solutions didn't work for me. If you are trying to reach out to an external database running unit tests can give some weird errors. For some reason they do not get cleared after running the test so you have to run rake db:test:purge after running the unit tests. Put this in your Rakefile and it should fix it.

Rake::Task["db:test:prepare"].enhance do
  Rake::Task["db:test:purge"].invoke
end
烟火散人牵绊 2024-08-05 04:07:34

我在我的机器上遇到了这个问题。 由于验证问题,我遇到了测试失败,因为数据库在测试之间没有正确重置。 关于我的情况的一些背景故事:

-I had a linux box, and was running code, that I knew should pass the tests.
-I bought a Mac with Lion installed and attempted to get my code running on that machine.
-I installed mysql from source

一切都安装良好。 我的数据库工作正常,rails 可以访问它。 然而,当我进行测试时,我遇到了同样的问题。 我遇到了这篇文章,并尝试了两种建议的解决方案(尽管它看起来不像代码问题,但它看起来像是配置问题,因为 rake 在我的 Linux 机器上运行良好)。 所有解决方案都不起作用。

我删除了 mysql:

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm /etc/my.cnf
sudo rm /usr/local/bin/mysql*

我用自制软件重新安装了 mysql,而不是从源代码手动安装(这一步是由同事的建议提供的):

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
brew install https://github.com/adamv/homebrew-alt/raw/master/versions/mysql51.rb
unset TMPDIR
mysql_install_db

然后我重新运行 rake,所有测试都通过了。 如果有人在 Lion 上,从源代码构建了 mysql,并遇到了这个问题,这可能是一个解决方案。

I ran into this problem on my machine. I was getting test failures, from validation problems because the database wasn't properly being reset between tests. Some back story about my situation:

-I had a linux box, and was running code, that I knew should pass the tests.
-I bought a Mac with Lion installed and attempted to get my code running on that machine.
-I installed mysql from source

Everything installed fine. My database worked, and rails could access it. When I ran tests however, I ran into the same problem. I came across this post, and tried both the proposed solutions (even though it didn't seem like a code issue, it seemed like a configuration problem since rake ran fine on my linux box). None of the solutions works.

I removed mysql:

sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm /etc/my.cnf
sudo rm /usr/local/bin/mysql*

I reinstalled mysql with homebrew instead of manually doing it from source (this step was courtesy of a co-worker's advice):

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
brew install https://github.com/adamv/homebrew-alt/raw/master/versions/mysql51.rb
unset TMPDIR
mysql_install_db

I then re-ran rake, and all the tests passed. If anyone is on Lion, built mysql from source, and ran into this problem, this might be a solution.

勿忘心安 2024-08-05 04:07:34

DB Cleaner 是一个非常好的宝石,专门用于测试之间的清理。 它提供了一些选项,包括将每个测试包装在事务中并回滚、截断表和删除。

如果您不使用/使用多个活动记录,它还支持多个 ORMS。

该文档非常好,包括将其与 MiniTest、Rspec 和 Cucumber 一起使用的示例。

https://github.com/bmabey/database_cleaner

DB cleaner is a pretty nice gem specifically for cleaning between tests. It gives a few options including wrapping every test in a transaction and rolling back, truncating the table, and deleting.

It also supports multiple ORMS in case you're not using/ using more than active record.

The documentation is pretty good and includes examples of using it with MiniTest, Rspec, and Cucumber.

https://github.com/bmabey/database_cleaner

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