cucumber / rails error uninitialized constant DatabaseCleaner (NameError)
Anyone have any idea what is causing this error when running cucumber features
?
un开发者_C百科initialized constant DatabaseCleaner (NameError)
Add this line to your 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.
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 test
This 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 requires database_cleaner
for you.
Otherwise, add database_cleaner
to your list of dependencies and put a require 'database_cleaner'
somewhere in your test suite code.
I just experienced the problem. I downgraded my cucumber gems to version 1.0.6, and I got this message:
uninitialized constant Cucumber::Rails::Database (NameError)
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):
rails g cucumber:install
It will prompt you to replace file features/support/env.rb
. Just answer with Y
and you can run rake cucumber:ok
again.
I am using spring, and spring stop
work for me
精彩评论