Situation
I am currently testing a Rails app using Capybara. In addition, I was using Guard with its extension guard-livereload to automatically reload my browser as soon as relevant source files changed.
As the save_and_open_page method from Capybara did not display stylesheets correctly, I applied this solution to the problem, in which a temporary view-dump capybara.html gets placed in the /public/ folder to ensure the accessibility of assets.
Now, as LiveReload has worked like a charm in development, I would like to use it during feature-tests to automatically reload /public/capybara.html instead of opening it over and over myself.
Problem
For some reason I can only insert the Rack Middleware, which is responsible for reloading the page, into the middleware-stack within the developent-environment, but not within the test-environment. I use the following code for insertion:
/config/environments/development.rb
Rails.application.configure do
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
end
When using the same method in /config/environments/test.rb, the following error occurs
myApp/config/environments/test.rb:44:in `block in <top (required)>': uninitialized constant Rack::LiveReload (NameError)
As I am still rather new to Rails, I don't really know where to start here. As far as I know, trying to require the file manually wouldn't really be The Rails WayTM .
So, how can I resolve this problem?
Thanks in advance.
In your Gemfile you are probably only loading the rack-livereload gem in the development group -- for this to work you would need to be loading it in the development and test groups. That being said, you really want your test environment to mimic production as closely as possible, so running rack-livereload in the test environment would usually be a bad idea.
I believe you should include the Rack::LiveReload in your test environment inside Gemfile:
group :development, :test do
gem "rack-livereload"
end
Related
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.
I am implementing a omniAuth authentication system through a gem and i would need to initialize a constant before the gem gets loaded.
Scenario:
The developer should write that constant in a config file, restart its server and that constant should be initialized before the gem gets loaded.
I tried to put it in a config/initializers/omniauth.rb file but i get a error when loading the server
unitialized constant OmniAuth::Strategies::Xyz::URL.
i am writting it in the gem in this manner:
OmniAuth::Strategies::Xyz::URL= "http://my_account.com"
If you want to set a constant before gems are loaded in a Rails app, you can place it in config/application.rb, just before the Bundler.require statement. However, since no gems have been loaded yet, you might run into trouble setting such a deeply nested constant.
The proper way to do this, is not to rely on constants for configuration. Make a proper configuration object. You can make a Railtie to add a proper configuration option to Rails itself and define the right hooks like to_prepare to start loading your gem's configuration at the right time.
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!
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.
I am trying to have a plugin I am developing auto-reload every time I change my code, emulating the same auto-reloading that happens normally in Rail's development mode. My plugin is primarily an ActiveRecord mixin module. I have tried all suggestions I have been able to find in related Google searches. Nothing has worked yet.
In my plugin's init.rb:
require 'activesupport' unless defined? ActiveSupport
require 'activerecord' unless defined? ActiveRecord
if RAILS_ENV == 'development'
ActiveSupport::Dependencies.load_once_paths.delete lib_path
ActiveSupport::Dependencies.load_once_paths.delete File.join(lib_path, 'crowd_compass', 'publisher.rb')
ActiveSupport::Dependencies.load_paths << lib_path
ActiveSupport::Dependencies.load_paths << File.join(lib_path, 'crowd_compass', 'publisher.rb')
end
ActiveRecord::Base.send(:include, CrowdCompass::Publisher)
Looking in the rails changelog, I did notice the feature to auto reload all plugins.
config.reload_plugins = true if RAILS_ENV == 'development'
This did not work as I expected it to when I added it to my conf/environment.rb
My plugin is structured so all files are auto-loaded by namespace => directory. I did this so I could avoid using "require", as I thought require was inhibiting my plugin from being auto-reloaded.
I have been doing all of my work in development mode through the rails console and I do not know if this behaves any different than running through mongrel (or like web server).
The plugin works as expected, but I have to reload every time I make any change to the code. Does anyone know a way to get plugins to reload?
The console definitely doesn't work like a mongrel. All of the techniques you're using are made to reload on every request, which is akin to every time you start up the console.
There isn't a way to reload code in the console without calling reload!.