Change rake test environment - ruby-on-rails

In my job we have a development, test and production environments. Development is the local environment use for development, test is a shared environment use for general purpose testing (QA) and production is well for production.
We are starting to development is Rails, in this framework the 'test' environment is use for running the specs. How do we specify the new environment to run the unit tests?
I have try to change the value of RAILS_ENV on "test_helper" but when running "rake test" it still try to connect to the test database.

Related

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.

How to test that Rails application works correctly in the production environment

I'm writting a rails application by TDD, So, I was wondering, how do I test production app, like having a different security token from development security token, how to ensure that all the configurations are correct in production, like mail configs etc.
Shouldn't we never run tests in production mode, since it would wipe the database.
So, how should one go about testing a rails app in production
The method many developers use is creating another environment called stage. You can make your configuration identical to production, and deploy to this before production to ensure everything works correctly. You can run your integration tests / performance tests against staging if you like.
You can copy the production config (config/environments/production.rb) to a new file called stage.rb, then remember to populate your database.yml with the new connection details for a stage database.
When you run tests you are always in the test environment since RAILS_ENV is hardcoded in test_helper.rb. Setting RAILS_ENV=production means the schema will be cloned from the production database rather than the development database. I could of course set up a development database on the production servers, but that doesn't seem to make sense.
Given all the differences there are between my development and production server - operating system, web server, database, gems etc. - I can't really feel comfortable deploying my application unless I have first run my test suite not only in development but also in production. Thanks to the beautiful and powerful API of Capistrano this is a walk in the park to accomplish:
Here goes the example how it seems to be done -
desc "Run the full tests on the deployed app."
task :run_tests do
run "cd #{release_path} && RAILS_ENV=production rake && cat /dev/null > log/test.log"
end
desc "Copy in server specific configuration files"
task :copy_shared do
run <<-CMD
cp #{shared_path}/system/voxway.rb #{release_path}/config &&
cp #{shared_path}/system/database.yml #{release_path}/config
CMD
end
desc "Run pre-symlink tasks"
task :before_symlink, :roles => :web do
copy_shared
run_tests
end

Running "rake test" in production environment?

I want to run unit tests in a Cron Job in a Production environment. Due to restrictions in the # of machines I have in my disposal, I can only run these units tests in 1 production machine. I don't have a development machine/environment. Let's imagine this is the case (even if it's not a realistic and a bad setup) and I can't change that at all :)
Is it possible to run "rake test" or even "rake test RAILS_ENV=test" in a production environment? And if I do, would it always run the tests on the test database, not on the production one? And it it does, would the environment be "test" only within the scope of the test?
Obviously, I could test this, but because I don't want to risk production data, I rather have a firm answer before doing this.
Yes, this will work just fine. Make sure you specify the RAILS_ENV environment variable like you already have and that you have the test database configured in your database.yml file.

CruiseControl.rb always running my projects in production mode?

I'm curious as to why when I build my project using CruiseControl.rb, it runs it in production mode? Even though my application should not be in production mode. I even tried to specify: ENV['RAILS_ENV'] ||= 'development' in my app's environment.rb
That environment.rb line will only set your environment to development if the environment is not already set.
For information about how they suggest configuring your build environments, see:
Their Manual Entry on the subject

Resources