黄瓜/rails错误未初始化常量DatabaseCleaner(NameError)
任何人都知道运行cucumber features
时导致此错误的原因是什么?
uninitialized constant DatabaseCleaner (NameError)
Anyone have any idea what is causing this error when running cucumber features
?
uninitialized constant DatabaseCleaner (NameError)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将此行添加到您的 Gemfile 中:
这是因为
cucumber-rails
不会自动依赖于database_cleaner
,因为您可能正在构建没有数据库的 Rails 应用程序,因此您必须明确要求它。Add this line to your Gemfile:
This is because
cucumber-rails
doesn't automatically depend ondatabase_cleaner
because you may be building a Rails application without a database, and so you must explicitly require it.DatabaseCleaner 是一个用于“清理”数据库的库。 Cucumber 将在运行功能之间使用它,以确保您的数据库处于可测试状态(即空)。
这个想法是,您在每个测试的
Given
子句中构建正确的数据。此错误仅意味着没有正确需要 DatabaseCleaner。
不同版本的 Rails/Cucumber 有不同的配置方式,并在这方面提供不同的功能,因此在不了解您的设置的情况下很难真正为您提供正确的解决方案。
不过有一些提示:
查看 cucumber-rails gem。它为您提供了许多好东西,例如生成器和 rake 任务,因此您可以运行 rake cucumber 而不是直接使用 cucumber。很多时候,生成器会构建一个需要
database_cleaner
的配置文件。否则,将
database_cleaner
添加到您的依赖项列表中,并在您的测试套件代码中的某处放置require 'database_cleaner'
。DatabaseCleaner is a library for 'cleaning' your db. Cucumber will use it between running features to ensure your db is in a testable state (ie. empty).
The idea is that you build up the proper data in your
Given
clauses for each testThis error just means that DatabaseCleaner hasn't been required properly.
Different versions of Rails/Cucumber have different ways of configuring everything and provide different functionality with this regard so it's hard to actually give you the right solution without knowing your setup.
A couple of tips though:
Look at the cucumber-rails gem. It gives you lots of nice stuff such as generators and also rake tasks so you can run
rake cucumber
instead of using cucumber directly. Often times the generators will build a config file that requiresdatabase_cleaner
for you.Otherwise, add
database_cleaner
to your list of dependencies and put arequire 'database_cleaner'
somewhere in your test suite code.我刚刚经历了这个问题。我将黄瓜宝石降级到版本 1.0.6,并收到此消息:
当我使用黄瓜 1.0.6(不是最新版本)和 database_cleaner v.1.7.0 时。为了修复错误,我只需运行此命令(在 Rails 3.1.3 上):
它将提示您替换文件
features/support/env.rb
。只需回答Y
即可再次运行rake cucumber:ok
。I just experienced the problem. I downgraded my cucumber gems to version 1.0.6, and I got this message:
when I use cucumber 1.0.6 (not the latest version) and database_cleaner v. 1.7.0. For fixing the error, I just run this command (on Rails 3.1.3):
It will prompt you to replace file
features/support/env.rb
. Just answer withY
and you can runrake cucumber:ok
again.我正在使用 spring,并且
spring stop
为我工作I am using spring, and
spring stop
work for me