cucumber / rails error uninitialized constant DatabaseCleaner (NameError) - ruby-on-rails

Anyone have any idea what is causing this error when running cucumber features?
uninitialized 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

Related

Rspec failing while upgrading RubyGem-rails to 4.2.x

I am upgrading rails from 4.1 to 4.2.x. I am facing multiple issues while upgrading it. As of now rspec is failing with following error :
Warning: you should require 'minitest/autorun' instead.
Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'
I have also added minitest in my Gemfile.
Can you please help me in figuring out on how this can be removed?
Update:
My rspec_helper.rb has require 'rspec/autorun'. I replaced it with require 'minitest/autorun'. I still run into same error.
require "rspec/autorun" was deprecated in RSpec 3, so is not the solution to your problem. You should remove it. You should also remove minitest from your Gemfile. It's likely being pulled in by a dependency, having you explicitly specify it won't change anything.
minitest and rspec are two separate libraries. The warning is not coming from rspec. I'm guessing you're just seeing it when you run your test suite. My best guess is that one of your test libraries is loading minitest some how. It's a big hammer, but you can try installing all your gems to a local cache bundle install --vendor and then grepping for minitest: grep minitest/autorun -R . to try and locate.

How to uninstall minitest from Rails

I'm using Ruby 2.2.0p0, and Rails 4.2.0. The Rails application is currently defaulting to using minitest, even though there's no testing gems mentioned in the Gemfile. For example, I have rake test in my rake tasks, and a test folder.
I can see how to install RSpec-rails. But what, if anything, do I need to do to uninstall minitest?
The only thing the RSpec-Rails README has is
Once installed, RSpec will generate spec files instead of Test::Unit
test files when commands like rails generate model and rails generate
controller are used.
It seems to me that in your case it not necessary to uninstall minitest, just add rspec to your Gemfile and start using it. However I recommend you to make RSpec as your default test framework.
Just add to config/application.rb next line:
config.generators.test_framework :rspec
That's it. Now Rails knows that you use RSpec and when you use Rails generators, it will add appropriate RSpec templete files, not Minitest ones. If you have test folder in your application (it's used by Minitest and Test::Unit) you can delete it now.

spork 0.9.2 and rspec 3.0.0 = uninitialized constant RSpec::Core::CommandLine (NameError)

Im using spork 0.9.2 and rspec 3.0.0. When trying to run test rspec --drb I have an exception
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/spork-0.9.2/lib/spork/test_framework/rspec.rb:11:in run_tests: uninitialized constant RSpec::Core::CommandLine (NameError)
But when changing rspec version back to 2.6 - everything is OK.
Has anyone faced the same issue? Is it possible to work around?
The reason is that RSpec::Core::CommandLine was removed in Rspec3
https://github.com/rspec/rspec-core/blob/master/Changelog.md
Merge RSpec::Core::CommandLine (never formally declared public) into RSpec::Core::Runner. (Myron Marston)
But spork depends on this code.
There is already an issue on spork's github and a solution can be found in a following spork's fork:
https://github.com/codecarson/spork/commit/38c79dcedb246daacbadb9f18d09f50cc837de51#diff-937afaa19ccfee172d722a05112a7c6fL6
In general - replace
::RSpec::Core::CommandLine.new(argv).run(stderr, stdout)
with
::RSpec::Core::Runner.run(argv,stderr, stdout)
in the soprks source code
Like #lx00st said:
The reason is that RSpec::Core::CommandLine was removed in Rspec3
The spork gem hasn't been updated in rubygems.org. However, the fix has been merged into spork's master branch on github. You can grab it by telling bundler that you'd like to get spork from github (master) instead of rubygems.org. So do this:
This has been fixed on spork's master branch. Simple solution:
gem 'spork', github: 'sporkrb/spork', branch: 'master'
If you're using spork-rails, just require spork via github before requiring spork-rails in your gemfile. For more info on this, see my comment here:
https://github.com/sporkrb/spork-rails/issues/26
Edit: added branch: 'master'
Same thing here. Just remove the "--drb" line from .spec file and remove the cli: '--drb' parameter on the guard :rspec... line within the Guardfile. This does not turn off spork. It just turn off the "distributed ruby" (--drb) Rspec option. As guard knows you are running Rspec through Spork, it is not needed.
I started using Spring instead of Spork and that solved it.
It seems to be the new Rails way:
http://edgeguides.rubyonrails.org/4_1_release_notes.html#spring-application-preloader
I had this same problem. Sans digging into the rspec3 source code, removing the --drb line from my .rspec file fixed the problem for me. Some Guardfile examples also have use of the --drb which causes issues for me. Once removed all tests work fine.

Bootstrap Devise Cancan Rails - Rspec Failure

I just installed Rails3 Bootstrap Device Cancan in my local machine and ran rake spec but I'm getting all test failures with below error in common:
undefined local variable or method 'postgresql_version' for #<ActiveRecord::ConnectionAdaptors::SQLite3Adaptor:0x489dff8>
I'm not sure why I'm getting this since I followed the instructions on https://github.com/RailsApps/rails3-bootstrap-devise-cancan carefully.
I'm currently running this on Windows 8 and used Bitnami RubyStack to run CMD
I ran into the same problem and this is how I resolved it.
It looks like there is a problem with version 1.1.0 of database_cleaner. See "database_cleaner >= 1.1.0 is broken for SQLite" (https://github.com/gregbell/active_admin/issues/2388). I updated my Gemfile to set
gem 'database_cleaner', '< 1.1.0'
then ran "bundle install" and after it completed, "rake spec" ran cleanly.
Double-check your database.yml. Refer to Configuring Rails Applications, Section 3.12, Configuring a Database.
Did you intend to use sqlite3? Is sqlite3 in your Gemfile? Naturally, SQLite3Adaptor will not respond to postgresql_version! Can you include a few more lines of the stack trace?
I doubt this has anything to do with devise or cancan.

Installing Mocha 0.9.7 in a Rails 2.3.3 project

I installed the Mocha 0.9.7 Rails plug-in using:
$ script/plugin install git://github.com/floehopper/mocha.git
(Just followed instruction in http://mocha.rubyforge.org/)
Then, I have the following set-up defined in my functional test
def setup
#controller.expects(:logged_in?).returns(true)
#controller.expects(:admin_user?).returns(true)
end
Running the test generates the ff. error:
NameError: uninitialized constant Mocha::Mockery::ImpersonatingName
/test/functional/xxxx_controller_test.rb:x:in `setup'
Before that, I see the ff. error at the top of the test log:
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.3/lib/active_support/test_case.rb:12: warning: already initialized constant Mocha
This has led me to believe that I have an old version of Mocha somewhere in Ruby's or Rails' path. The problem is I can't find it.
Is my guess correct? If so, where is this old version of Mocha? Alternatively, how can I found out where it is?
According to http://selfamusementpark.com/blog/2009/07/30/rails233mochaconfusion/, my guess is not correct. The problem really is that Mocha is being loaded before the testing framework which is not what the former expects. The solution is to edit RAILS_ROOT/vendor/plugin/mocha/init.rb to comment out the ff. line:
require 'mocha'
Then, Mocha will have to be explicitly required in the test files or helpers to ensure that the testing framework will have been loaded beforehand.

Resources