When To Use A Particular Rails Environment for Testing? - ruby-on-rails

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.

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"

Run single system test

To run a single test in Rails, we normally do:
rails test TEST=test/system/invitation_test.rb
But that doesn't work with system tests. Neither do this work:
rails test:system TEST=test/system/invitation_test.rb
With both those commandos above, all system tests (files) are run.
So my question is, how can I run a single system test?
As a side note, to run (all) system tests in Rails, you need to append :system to test.
rails test:system
While rails test doesn't seem to work if you want to run your system tests (you need to append test with :system), if you only want to run a single test it does seem to work:
rails test test/system/my_little_test.rb

Change rake test environment

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.

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.

Resources