Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cucumber / rails error uninitialized constant DatabaseCleaner (NameError)

Anyone have any idea what is causing this error when running cucumber features?

uninitialized constant DatabaseCleaner (NameError)
like image 867
DanS Avatar asked Mar 29 '11 20:03

DanS


3 Answers

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.

like image 72
Ryan Bigg Avatar answered Oct 23 '22 00:10

Ryan Bigg


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.

like image 4
brad Avatar answered Oct 23 '22 00:10

brad


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.

like image 1
rakhmad Avatar answered Oct 23 '22 00:10

rakhmad