Loading testing environment in rails - ruby-on-rails

I am trying to load the test environment for unit testing and used several commands like
1. RAILS_ENV = test
2. rails console test
3. RAILS_ENV=test rails console
I am getting the same error each time(undefined method 'symbolize keys').
Can someone let me know what to do to load the test environment ?

If you are using automated testing, your test environment will be loaded automatically.
If you want to switch to the test environment in your console:
rails console test
The error you get is telling me there is something wrong in your code.

Related

rails / check eager_loading

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"

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.

What is the correct way to run Rails tests without invoking the development environment?

I have an Rails app, with some initializer code which must be executed when the app is running in development mode. However, this initializer code must not be executed when running tests.
I have established that
$ rake test
causes the app to be run in development mode, which invokes the initializer code and therefore breaks my tests. This is expected behaviour apparently (see: https://github.com/rails/rails/issues/9801).
What is the correct command to run my Rails app tests without starting the app in development mode?
Does your test_helper.rb file look like the default? It should start with:
ENV["RAILS_ENV"] = "test"
Try running the rake task with an explicit environment:
rake test:units RAILS_ENV=test
If you don't specify an environment, development is assumed, in my experience. And while the test database still gets the fixture data inserted into it, stuff from the development environment still gets referenced for some reason.
You need to have a line at the start of the test_helper.rb file that says
Rails.env = "test"
You can't use
ENV["RAILS_ENV"] = "test"
because it will fail to clear the cached value that returns from calls to Rails.env.

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'

Resources