Member-only story
Test Driven Development in Ruby on Rails using Rspec
3 min readOct 12, 2020
Test Driven Development (aka TDD) is where you create test specs of expected outcomes. You can then code your way to pass these tests. The opposite of this method of coding is Behavioral Driven Development (BDD).
Setting up
First thing we need to do is install the ‘rspec-rails’ gem. We want this in the development test section in our gem file.
We can then create our rspec files using
rails g rspec:install
For this example let’s pretend we have 3 models. User, Cart, and Item.
When we use:
rails g resource Model attribute:type
The rspec file will be created for that model. To manually generate the file we can use
rails g rspec:model model_name
Creating a test
Lets create a test that totals all items in a users cart. Inside the user_spec.rb