简体   繁体   English

cucumber / rails错误未初始化的常量DatabaseCleaner(NameError)

[英]cucumber / rails error uninitialized constant DatabaseCleaner (NameError)

任何人都知道在运行cucumber features时导致此错误的原因是什么?

uninitialized constant DatabaseCleaner (NameError)

Add this line to your Gemfile: 将此行添加到您的Gemfile:

gem 'database_cleaner'

This is because cucumber-rails doesn't automatically depend on database_cleaner because you may be building a Rails application without a database, and so you must explicitly require it. 这是因为cucumber-rails不会自动依赖于database_cleaner因为您可能正在构建没有数据库的Rails应用程序,因此您必须明确要求它。

DatabaseCleaner is a library for 'cleaning' your db. DatabaseCleaner是一个用于“清理”数据库的库。 Cucumber will use it between running features to ensure your db is in a testable state (ie. empty). Cucumber将在运行功能之间使用它以确保您的数据库处于可测试状态(即空)。

The idea is that you build up the proper data in your Given clauses for each test 这个想法是你在每个测试的Given子句中建立适当的数据

This error just means that DatabaseCleaner hasn't been required properly. 此错误仅表示未正确地要求DatabaseCleaner。

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. 不同版本的Rails / Cucumber有不同的配置方式,并提供不同的功能,因此很难在不知道设置的情况下为您提供正确的解决方案。

A couple of tips though: 但有几个提示:

Look at the cucumber-rails gem. 看看cucumber-rails宝石。 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. 它为你提供了很多很棒的东西,比如发电机和耙子任务,所以你可以运行rake cucumber而不是直接使用黄瓜。 Often times the generators will build a config file that requires database_cleaner for you. 通常,生成器会为您构建一个需要database_cleaner的配置文件。

Otherwise, add database_cleaner to your list of dependencies and put a require 'database_cleaner' somewhere in your test suite code. 否则,将database_cleaner添加到依赖项列表中,并在测试套件代码中require 'database_cleaner'某处放置一个require 'database_cleaner'

I just experienced the problem. 我刚刚遇到了这个问题。 I downgraded my cucumber gems to version 1.0.6, and I got this message: 我把我的黄瓜宝石降级到版本1.0.6,我收到了这条消息:

uninitialized constant Cucumber::Rails::Database (NameError)

when I use cucumber 1.0.6 (not the latest version) and database_cleaner v. 1.7.0. 当我使用黄瓜1.0.6(不是最新版本)和database_cleaner v.7.7.0。 For fixing the error, I just run this command (on Rails 3.1.3): 为了修复错误,我只运行此命令(在Rails 3.1.3上):

rails g cucumber:install

It will prompt you to replace file features/support/env.rb . 它将提示您替换文件features/support/env.rb Just answer with Y and you can run rake cucumber:ok again. 只需回答Y ,你可以运行rake cucumber:ok

我正在使用弹簧, spring stop为我工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM