how to set up nulldb in rails with rspec? - ruby-on-rails

I'm almost new to rails development. I'm currently reading Avdi Grimm's Objects on Rails for having a #SOLID design in my rails apps instead of being forced to some conventions which will create mess and unreadable code and design.
I wanna setup nulldb and use it in my fast specs which are testing the business logic of the application but I can't get it to work. I read the installation guide at nulldb GitHub page here -> https://github.com/nulldb/nulldb. I installed the activerecord-nulldb-adapter gem and put it also in my Gemfile and ran the bundle install command so it's completely installed now. I have a spec_helper_lite.rb file which I use in my fast specs so I though it's a good idea to setup nulldb in it. Here's the code for nulldb part in my spec_helper_lite.rb file:
require 'nulldb_rspec'
def setup_nulldb
schema_path = File.expand_path("../db/schema.rb", File.dirname(__FILE__))
ActiveRecord::Base.establish_connection(:adapter => :nulldb,
:schema => schema_path)
NullDB.nullify(:schema => schema_path)
end
def teardown_nulldb
NullDB.restore
end
then I require this spec_helper_lite.rb in my fast specs and I call the setup and teardown nulldb method in before and after methods of my spec. when I run the specs I'll get the error Uninitialized Constant ActiveRecord::ConnectionNotEstablished (NameError). I tried different things like removing that establish_connection line in the setup_nulldb and I'll get the same error. I even required 'active_record' in my spec_helper_lite just to see what will happen and then I'll get the error "undefined method nullify" for NullDB module and obviously the spec became completely slow cause of requiring active_record. I searched a lot about how to setup nulldb and everything I saw explained about setting it up this way but it doesn't work for me. I use nulldb version 0.2.1, rails 3.0.0 and rspec 2.0.1
I appreciate your help about how to set this up correctly. Thanks in advance.
Sam

I was having similar issues (but using minitest, and activerecord independent of rails). I tried all the same stuff you mentioned. Turns out I was just using whatever was in rubygems. It looks like updating my Gemfile to pull directly from github resolved the issue:
group :development, :test do
gem 'activerecord-nulldb-adapter', :git => 'git://github.com/nulldb/nulldb.git'
end
You'll want to bundle install again after updating.

Related

ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base with mongoid

I have a Rails 4.2.8 app, ruby 2.4.4 with a Mongo database, using the mongoid ODB.
I want to run tests against this application using rspec. The most basic test gives me this error:
ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base
My research on this problem lead me to these conclusions, but I do not know how to solve the problem itself:
why is active record mentionned here, when it is not in my gemfile ? Maybe another gem is loading it at some point, but how can i get rid of this problem ?
my application.yml and mongoid.yml file are correctly configured. I can RAILS_ENV=TEST rails console and I can query documents.
looks like rails is going inside the condition line 15 in this file. So how and why is ActiveRecord loaded at some point ?
Some help would really be appreciated as I do not know what to do next...
Ok so I did not figure out who was loading ActiveRecord in my application, but I manually unload it in my rails_helper.rb:
Object.send(:remove_const, :ActiveRecord)
# Make sure it is above this
require 'rspec/rails'
I ran into the same issue and found that the problem was database cleaner. Previously we were using the gem database_cleaner but that gem seems to have been split based on your database adapter. Switching to database_cleaner-mongoid resolved the issue. Note that you may need to change some requires or config values to get things running correctly after the change. For instance I had an error about my strategy not being supported in mongoid.

Loading Rails project's app paths in Rspec

I'm writing an application in Rails 5 and using Rspec for tests. I'm having a lot of trouble with require 'some_model' or require 'some_controller' in my spec files. Every time I run a spec, it acts lost and doesn't seem to understand that it's in a Rails app. The suggestions I've seen range from
config.autolad_paths += %W( #{config.root}/app )
to
$LOAD_PATH << '~/my_app/app/models' which sort of works, except that my tests throw an unitialized constant for ApplicationRecord
So for all the digging around I've done, I still haven't found a good way require my MVC files while keeping my specs and spec_helper clean. There doesn't seem to be any defacto way to do so, so how do you all suggest doing it?
can you not take on the entire stress of RSpec configuration. Use the rspec-rails gem and look through the documentation to use it within your app. I guess you can get a base config with rails generate rspec:install after including the gem in your project.

How to get other Gem helpers into my Engine Gem's tests?

I'm trying to write tests for an engine gem I'm writing. I'm using Rspec and the tests seem to be running fine. But whenever a view uses a helper from another gem, such as "will_paginate" or "ransack", I get an "undefined method" error.
I've tried including the other gems in my gem's Gemfile (in addition to the engine.gemspec file) as well as the dummy app's Gemfile, but I get the same error. I've also tried including the gems in the spec/spec_helper.rb file.
So I've tried most of the things mentioned here:
Setup RSpec to test a gem (not Rails)
Usually, for Rspec tests for a regular Rails app, these helpers seem to be just included some how since I don't have this issue running tests for a regular Rails app.
I also have been needing to namespace my url helpers in the views with something like:
engine.resources_path
I'm not sure if that's a symptom of some configuration I've messed up on.
Everything in the engine runs fine when mounted to another app and viewed on the browser.
Any ideas?
Turns out a better approach is to stub out methods from gems since the gem should be testing their own methods anyways. Please let me know if I'm misunderstanding anything. Thanks!

Problem creating Rails 3 Engine

I'm working on creating my first Rails 3 engine and I'm currently getting the following error message
is a Railtie/Engine and cannot be installed as plugin (RuntimeError)
To give you a bit more background I'm not doing the Engine as a gem, but I've place the Engine files in the vendor/plugins folder. I know there is an issue with loading when in the plugins folder, but I'm not clear on how to solve the problem.
My rails/init.rb file in the Engine has the following code
require 'rails'
module RailsApp
module MyEngine
class Engine < Rails::Engine
config.after_initialize do
RailsApp::GameType.register do |game_type|
game_type.name = "TreasureIsland"
game_type.version = "0.1"
game_type.thumbnail = "teasure_island.jpg"
end
end
end
end
end
Suggestions?
I think I remember reading that Railties would not work in plugins directory, because they are intended to be loaded at a different point in the application boot process. I would recommend creating a gem using something like Jeweler, which does alot of the work for you. The purpose of the Railtie/Engine is to have a reusable component that can be included in multiple rails apps. Once you have a gem created, you can point to the local gem path within your Gemfile. This allows you to see changes in your engine code inside your rails app without having to build and reinstall the gem every time you make a change to the engine code. Of course you would want to point bundler to the installed gem in production. I would recommend putting it on github and using that URL in your Gemfile in production.
Bundler local gem example:
#Gemfile
gem "my_engine", :require => "my_engine", :path => "/my_engines/my_engine"
Check out the Modern Rubyist's website. He has a good series on creating Railties and Engines. There may have been some changes to Rails since this was written, but I think most of it is still relevant. It helped me out a good bit when I was learning how to write Engines with Rails 3.
http://www.themodestrubyist.com/2010/03/01/rails-3-plugins---part-1---the-big-picture/
http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
http://www.themodestrubyist.com/2010/03/16/rails-3-plugins---part-3---rake-tasks-generators-initializers-oh-my/
http://www.themodestrubyist.com/2010/03/22/rails-3-plugins---part-4---more-on-generators/
John, I believe engines (which are typically gems) vs plugins (which live in vendor) are loaded at different points in the rails initialization process.
Engines actually have a bit more flexibility, they can hook deeper into rails. In addition, packaging as a gem has a lot of advantages: easier to share across apps, easier to maintain in a separate code repo, easier version control.
I'm creating my first rails engine right now and created a useful starting point and walk-through for getting started:
http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/

Installing and using acts-as-taggable-on

This is going to be a really dumb question, I just know it, but I'm going to ask anyways because it's driving me crazy.
How do I get acts-as-taggable-on to work?
I installed it as a gem with gem install acts-as-taggable-on because I can't ever seem to get installing plugins to work, but that's a whole other batch of questions that are all probably really dumb. Anyways, no problems there, it installed correctly.
I did ruby script/generate acts_as_taggable_on_migration and rake db:migrate, again no problems.
I added acts_as_taggable to the model I want to use tags with, started up the server and then loaded the index for the model just to see if what I've got so far is working and got the following error: undefined local variable or method `acts_as_taggable' for #.
I figure that just means I need to do something like require 'acts-as-taggable-on' to my model's file because that's typically what's necessary for gems. So I did that hit refresh and got uninitialized constant ActiveRecord::VERSION. I'm not even going to pretend to begin to know what that means went wrong.
Did I go wrong somewhere or there something else I need to do. The installation instructions seem to me like they just assume you generally know what you're doing and don't even begin to explain what to do when things go wrong.
Did you try to define your gem dependencies in config/environment.rb (Rails 2.3):
Rails::Initializer.run do |config|
#...
config.gem 'acts-as-taggable-on'
#...
end
Or in Gemfile for Rails 3 or if you use already Bundler with rails 2.3:
gem 'acts-as-taggable-on'
This should make the require 'acts-as-taggable-on' unnecessary
Maybe following the installation here can help.
For example you don't need to:
require 'acts-as-taggable-on'
but:
class User < ActiveRecord::Base
acts_as_taggable
end
Otherwise you need to post more details about the error.
I installed acts-as-taggable-on for my app through github. If you want to try that method instead of the gem, you can read my this post that explains my experience: http://blog.mediummassage.com/2010/04/27/creating-categories-in-the-store-with-tags/

Resources