This is a Makers Vine. Vines are designed to gradually build up sophisticated skills. They contain a mixture of text and video, and may contain some challenge exercises without proposed solutions. Read more about how to use Makers Vines.
Learn to setup a Ruby project that connects to a database.
In this series of challenges you will build a few different projects. Before we get started, you'll need to set up a new RSpec project.
This project will use a few components:
- Ruby and RSpec
The programming language and test framework will be the same as what you've previously used. - The
pggem
This library allows us to send SQL queries to the database, and retrieve the result set. - A class
DatabaseConnection
This class acts as a thin layer with methods to connect to PostgreSQL and send SQL queries to it.
To set up your project follow this guide.
Follow the guidance linked above to create a new project music_library. Then move on to the next step.
A video demonstration is here..
We don't want our RSpec tests to use and modify with the data used by our program users. Therefore we need to create a new database which will be used only by RSpec tests. Such a database is usually suffixed by _test. Let's create a test version of the music_library database:
# In your terminal
createdb music_library_testThen make sure you connect to this database in your spec_helper.rb file:
# file: spec/spec_helper.rb
DatabaseConnection.connect('music_library_test')