Adding a database
Adding a database
The time has come for us to add a database and to start defining our objects. At this point our application will start to grow more complex.
We will:
Introduce a new testing framework to test our objects -
RSpecIntroduce a database -
PostgreSQLIntroduce a Object Relation Mapping (ORM) library -
DataMapper
Add the following gems to your Gemfile
# Gemfile
gem 'data_mapper'
gem 'dm-postgres-adapter'
gem 'pg'Also, we need to start grouping our gems in that Gemfile. Please create the following group:
# Gemfile
group :development, :test do
endAdd the following gem to that group:
The dm-rspec gem extends RSpec with a set of matchers for DataMapper objects. Having access to those matchers will make our testing much easier.
Also, I would like you to move all gems that we use for testing purposes to the :development, :test group.
Your Gemfile should look something like this:
Don't forget to do bundle install once you have made all the changes.
In terminal run the following command:
You should see the following output:
Now, open .rspec file (it is located in your main project folder but it is a hidden file, so your text editor might not show it) and modify it so that the first line is set to:
Now, in your terminal, type in rspec and hit enter.
The output you see should be something like:
We are not quite done yet. Sorry. :-(
I want you to open the spec/spec_helper.rb file. You'll se a lot of lines. Most of them are not needed so I would like you to delete all lines that begin with the #sign. These are comments and we don't need them - for now, they are just a distraction. .
When you are done you should see something like this:
Not so bad, ey?
Alright, we need to add some of the libraries/gems we want to use in our spec_helper. Modify your file to include:
...and in the RSpec.configure block, add:
Now, run rspec again and you should receive no errors.
Okay, this means that you have successfully added and set up RSpec as a testing framework. The next step will be to start writing some tests for our Course class.
More on databases
A database is an organized collection of related data, typically stored on disk, and accessible by possibly many concurrent users. Databases are often separated into application areas. For example, one database may contain Product information data; another may contain sales data; another may contain accounting data; and so on.
More on databases and Relational Databases
Last updated