ruby-on-railstest-environmentsrake-test

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?


Solution

  • 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.