Thursday, October 26, 2006

Changing Rails Environment During Tests

Most rails developers know that rails encourages a testing culture, right from when you generate your controllers. Rails creates a test folder which contains subfolders for unit tests, functional tests. It also creates a fixtures subfolder and mocks subfolder.
Rails also creates three enviroments to operate in, namely the development environment, production environment and test environment. They all require separate databases to run from.

The fixtures subfolder will contain sample data to be used in testing and rails defaults the format of this data to YAML. Fixtures can contain data to be used for various test cases for an application. The following is an example of a fixture:

Normal Registration
first_name: Oliver
last_name: Jintha
Date_of_birth: 1982-04-17

Awkward Registration
first_name: 0liver
last_name: Jintha
Date_of_birth: 1982-04-17

Extreme Registration
first_name: Oliver
last_name: Jintha
Date_of_birth: 2982-04-17


Having defined the fixture and written the appropriate unit test for it, the next thing would be to load the fixture data and then to run the actual test.

Loading the data can be done by the command:

rake load_fixtures

This will however load the fixtures in your default enviroment and it will delete the data in your default database, as such, it is required that you specify the environment in which to run before proceeding with loading the fixtures.

This can be done by this command:

export RAILS_ENV=test

Then you can load the fixtures using:

rake load_fixtures

This will not delete your data in the default database as it did in my case.

Literature Cited
Dave Thomas, David Heinemeer Hansson(2006),Agile Web Development with Rails,Raleigh,North Carolina Dallas, Texas(USA), Pragmatic Bookshelf.

http://manuals.rubyonrails.com/read/book/5

No comments:

Efficient markets may determine self-sufficiency at the micro level for farmers

Watching dedication of farmers and their enthusiasm as they tend their gardens in various parts of the country, refreshes my mind as the p...