Creating a rails application with rspec
In this document, you will complete the following tasks:
- Create a rails application directory
- Discuss the rails application structure
- Vendor rails to a specific version
- Install rspec and rspec-rails gems
- Configure rspec and rspec-rails gems
REQUIREMENTS
Prerequisite knowledge
This instructional document does not require that you understand Ruby on Rails. However, familiarity with osX or Ubuntu (debian) Linux will be helpful. This document is not designed for use with Windows. This instructional document is designed to be completed from top to bottom. More information can be found on this topic at One-Click Ruby Installer.
CREATE A RAILS APPLICATION DIRECTORY
To start with development, we are required to create a rails application directory. Go ahead and execute the following commands:
$ cd /some/directory/for/your/projects
$ rails starter-project
$ cd starter-project
DISCUSS THE RAILS APPLICATION STRUCTURE
Every rails application contains a standard group of directories and files. You can think of this structure as a skeleton for your application. Now let’s discuss each directory and how it is used.
app
Rails follows the Model, View, Controller (MVC) design pattern for developing applications. This directory holds a controllers, models, and views directory. Helpers is available for reuse of code in the rails views.
config
As the directory states, this is where configurations lie
- Database configurations – database.yml
- Settings for all environments – environment.rb
- environments provides a place for development, testing, and production configurations
- initializers holds files that contain blocks of code run at startup time
- locales holds localization files
- Website Url class mappings – routes.rb
db
- SQLite db files
- Database Migrations (after first migration is generated)
doc
Document directory
lib
User created libraries.
- Tasks includes *.rake files for creating rake commands
log
Log files
public
Html, image, javascript, and stylesheet resources
scripts
Commands to start a web server, generate objects, destroy objects, and more
test
Testing for unit, functional, integration, performance and additionally fixtures
tmp
Temporary directory for pids, sessions, etc
vendor
Plugins, Gems, and specific versions of the rails framework
VENDOR RAILS TO A SPECIFIC VERSION
We can save a copy of the rails gems inside our application. The benefits of doing this are keeping a specific version of gems bundled with the application. This helps in deployment and building a development environment on another computer.
$ rake rails:freeze:gems
INSTALL RSPEC AND RSPEC-RAILS GEMS
We will be using rspec for our testing framework. It provides a language centric way of testing where the code is meant to be readable by developers and well as the business/clients.
To install the framework, we need both the rspec and rspec-rails gems. rspec is a spec testing framework for ruby and rspec-rails offers some helpers for rspec in a rails application.
$ sudo gem install rspec
$ sudo gem install rspec-rails
CONFIGURE RSPEC AND RSPEC-RAILS GEMS
Next we need to tell the application how to reference rspec. To do that, add the following lines inside your config/environment.rb file.
# in config/environments/test.rb
config.gem "rspec", :lib => false, :version => ">= 1.2.0"
config.gem "rspec-rails", :lib => false, :version => ">= 1.2.0"
Now regenerate the rspec files
$ script/generate rspec
Your application is now setup with the rspec library! If you wish to continue in this series, the next article will discuss building your models and schema with migrations.