rails / check eager_loading - ruby-on-rails

In development mode, eager_loading is disabled
Sometimes, syntax error occur, and having eager_loading set to false does not help to identify those. Is there a rails command (rake tasks) that could trigger such check ?
The point here is not to run test to find out, but to setup/use a command that would provide a similar result as a rails startup in production

In case your app is not 100% test covered and some classes / setup might break in case eager_login is enabled (eg. production mode)
You can emulate such verification with the following (useful for pipelines before tests for example):
RAILS_ENV=production SECRET_KEY_BASE=xyz rails runner "some rails console command"

Related

Travis builds are failing with fatal listen error

My Travis tests for a Rails app have been working fine, but have suddenly started failing about one time in three with:
$ bundle exec rails test
FATAL: Listen error: unable to monitor directories for changes.
Visit
https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers
for info on how to fix this.
Looking at that suggested URL, it proposes ways to increase the number of inotify-watchers, but requires the use of sudo to change the limit. That's fine on my dev machine (though I'm actually not experiencing the error on my machine), but I don't know if that's possible (or advisable) in the Travis environment.
I looked at the Travis docs to see if there's a config setting for increasing the number of watchers, but I couldn't find anything.
So: what's the best way to deal with this error in a Travis CI test?
If you're running this on TravisCI and a CI/staging/testing server, you should not need to watch files for changes. The code should be deployed to the server, and then bundle exec rails test should run and that's it. No file watching necessary.
What I suspect is that the config for your environment is not set up correctly and that listen gem is somehow activated for the testing environment when it should only be activated for the development environment.
Try running the tests locally with the same environment as TravisCI (testing in this example):
RAILS_ENV=testing bundle exec test
and see what it says. If it gives you that error, check the config/environments/testing.rb file and look for config.cache_classes.
When config.cache_classes is set to true, the classes are cached and the listen/file-watcher will not be active. In your local development environment, config/environments/development.rb, the config.cache_classes setting should be set to false so that file-watching and reloading happens.

Can't turn off debugger in rails

I wanted to debug a test (MiniTest) in my Rails app. Therefore I followed the instruction on the Minitest-Debugger Github Page. Here is the important snippet:
Add this to the top of your test file or helper:
require 'minitest/debugger' if ENV['DEBUG']
Then run your tests as normal but define DEBUG=1:
% DEBUG=1 rake test
Debug.rb
Emacs support available.
Since then, all rails commands (like rails server or bundle exec rake test) I type into the console from my app directory try to start the debugger, which doesn't even work right but gives me some error messages from classes / parts outside my app.
% rails s
Debug.rb
Emacs support available.
/Users/chris/.rvm/gems/ruby-2.0.0-p353/gems/binding_of_caller-0.7.2/lib/binding_of_caller/mri2.rb:25: `' (NilClass)
How can I reset my environment / Rails / Ruby so it runs normally. I tried of course:
% DEBUG=0 rails s
But that didn't change anything. I deleted everything I set up in order to debug.
What do I have to do to stop Rails from entering debug-mode?

When To Use A Particular Rails Environment for Testing?

I am teaching myself Ruby On Rails with Michael Hartl's Book. When it uncovered the use of seeds.rb file, I tested within Development Environment, it Failed. When set to Test Environment, It Succeeded. Why? When will I need to change the environment again for successful tests?
When you say I tested within Development Environment, it Failed., you are not running automated tests. You ran the rake db:seed script against the development database. The same task can be run against the test environment with rake db:seed RAILS_ENV=test. Again, this is not an automated test.
There are many reasons why rake db:seed run against the development environment failed in your case. The specific reason could be identified based on the error message.
development environment is one where you work on a day to day basis, adding/changing functionality by making code changes. By default, most scripts assume that you are working with development environment.
test environment is the environment against which the automated tests are run. In the case of rails tutorial, the automated tests are written in the files under test folder. When automated tests are run on a rails application - with rake test or some other way - the test environment is used to run these tests against. The test database gets cleaned up before running the tests to ensure the tests are run starting with a blank state.
Hope this clarifies.

Problem using `rake test`

I wonder how to setup testing in my rails apps. When I run rake test, first thing odd, it launch a bunch of CREATE TABLE against my dev. database (hum.. do not like this..). So I launch rake test RAILS_ENV=test and I even try bundle exec rake test RAILS_ENV=test. Now, the CREATE TABLE is against my test database but all fails with this error :
** Execute test:units
test/unit/category_test.rb:5:in `test': unknown command 't' (ArgumentError)
from test/unit/category_test.rb:5:in `<class:CategoryTest>'
I have used basic generator in Rails 3 and do not change anything. So I have this in caterogy_test.rb :
require 'test_helper'
class CategoryTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
I use Rails 3.0.7 and basic config.
Any ideas ?
EDIT
I am becoming crazy, made a lot of tries, neither seems to work. When I start a new application with a few things, rake test works fine but when I try this on my current one, it launch always against my dev. db and do not work at all. I have tried to edit the test files, to revert them back, try to remove/setup test db with different ways, try different rake version, compare a lot of things on one side my current application and on the other a brand new one... Found nothing.. Help !
EDIT 2
Sounds lame, but is it normal that rake does the same thing than rake test ?
EDIT 3
Sounds odds, while I continue to work on what's wrong, I realize that every-time I run rake test, it does stuff on the dev environment and not the test one (watching the logs). It does this on my computer OSX and on our server FreeBSD for all the Rails 3.0.7 apps. Are you sure rake test is supposed to work on the test environment by default ?
EDIT 4
Please help!
EDIT 5 - SUMMARY
When running rake test in my computer or on our server in Rails 3.0.7 with different apps it does the following :
run CREATE TABLE and INSERT INTO migration against the dev. db.
do not empty the dev. db.
development.log gets written not the test.log
also an issue with the error unknowm comman 't' with one specific app.
EDIT 6 - db config
Nothing change from the default yet : https://gist.github.com/1006199
EDIT 7
rake db:test:prepare --trace -> nothing break (but keep printing (first_time)
https://gist.github.com/1007340
With RAILS_ENV="test" for rake, everything goes fine. It write on the test logs.
ruby -I test test/unit/category_test.rb same erros than with rake, but no write on the dev. or test logs.
a bunch of unorderd answers:
the "CREATE TABLE" statements usually means that your test_db is created from scratch (by default, before test task, a db:migrate is launched). are you sure they're called on dev_db?
also check your config/database.yml to see if there's some typo (eg: using same table for test and dev environments)
it looks like there's an error in some of your migration files (that 't' error remember blocks in migrations).
"rake test" is the default task, that's why it's run when you just launch "rake" without arguments.
EDIT:
according on what I see on edits, from 5 and above, it looks like you have some issue with environment files. so try to double-check:
* config/environments/test.rb
* config/application.rb
* config/environment.rb
if with RAILS_ENV="test", everything goes fine, then I'm almost sure you have changed some default behaviour in your app (configs, env variables, any particular gem?)
also, in your test/test_helper.rb, add RAILS_ENV='test' at the beginning of file, this should force test environment.
I had that same error message, except to me it said: in `test': unknown command 'i' (ArgumentError).
The 'fix' or 'workaround' was to simply use:
$> bundle exec rake test
instead of using 'rake test'

Start up required additional services (resque, redis) with `rails server` command

I would like my development environment for Rails to automatically start redis and resque (and potentially in other projects, mongod, mysql-server etc.) for me, in the following cases:
When starting up the development server rails server.
Additionally, it would be nice if the following cases detect already running services, and, if not running start them up too:
Rake rspec, rspec /spec, when running tests.
When starting up a rails console.
When shutting down the rails server, the started child-services should be shut down too.
What is the correct place for such additional startup scripts?
And how to avoid them being started in production too (where I run everything trough /etc/init.d services)?
A lot of these built-in tasks are available as rake tasks already.
You can create a master rake task that does it all.
For example, with resque, you get "rake resque:start" "rake resque:scheduler:start", etc.
You can create a generic "start" task that depends on the rest. Similarly, a "stop" task would shut everything down.
So you would do:
rake start # starts all associated processes
rake stop # stops them all
This is also very use to use from Capistrano, when you end up deploying your code somewhere else. Rake tasks are very easy to call from Capistrano.
I think it's really better to do that in some external script. Do it in your rails server command can be really annoying to anyone to try your code.
By example, in one year, a nez developper come to your project. He can be desoriented if your rails server commande launch a such of other application in background.
In same idea, if you do that you need maintain your code in your rails env. Can be a little tricky. Maintain an independant script can be more usefull.
You can add your script in script directory. That be a good pratice. But not when you launch a command with a manual who do not that.

Resources