A Beginner Friendly Guide on How to Incorporate Test Driven Development (TDD) Into A Ruby Application Using the RSpec Gem
Tags: ruby, testing, rspec, tutorial
Published: 2022-10-05 23:46:51


I'm sure most of you out there have heard of test-driven development, but a lot of you aren't quite sure how to implement it or where to even get started. For those of you who don't know test-driven development is, TDD is a software development practice that relies on creating test cases for all of your deliverables before you have fully developed your application. It's a great method to keep your code bug-free and I highly recommend you to implement in your work if you haven't already.
RSpec is a ruby gem quoted as "Making Test-driven Development Productive and Fun". And I definitely agree! It's incredibly easy to get started with too, as it's meant to be as human-readable as possible.

Go Ruby!

To get started with RSpec, install the gem and create a new file. I called mine triangle.rb. Now you need to make sure to make a gemfile and install RSpec using

```bundle install --binstubs```
if you haven't already, then to kick things off
```rpsec --init```
Since our file is empty and we need something to test, copy this code below.

![triangle class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7iseqo66iwo6cbwelz9d.png)


Now we can create a test for our class and write some tests or our equilateral_perimeter method before we write it. Create a file called test.rb in your spec folder.

An equilateral triangle's perimeter should be 0 if a side length is 0 because a length of 0 is impossible.

![test example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/goxlulbeqqg9v7nzw3hu.png)

Here's a basic test case. The important methods to take note of here describe, context, and expect. Their names are meant to be human-like syntax.

To start a test with RSpec, call the method describe, which takes the name of the class, and the method to test.

Let's see what our test returns and then we can break it down. To do this type

```rspec path/to/your/file```
in your terminal

![first test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42oh2ezjstdcdvmw6d9c.png)


Failure! Amazing!
**Context** here is a way of grouping cases. You should make it intentionally broad so you can think of more test cases as time goes on. Here I chose 'when the perimeter is invalid' rather than 'when the perimeter is 0'.

The **it** method describes what the return value is supposed to be. This is optional but I would recommend setting up your tests like this to keep it neat.

Lastly, expect is where you call the method, and the expected value should go after using **eq()**.

Let's add another test using **it**.

![second test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33kv8oz8au3sp5x4etlf.png)


Awesome, looks like our tests are working. Now we can continue onto the next step of test-driven development and actually start making our functions!

![first method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0syqmlp9zmlsov3n0gm.png)


Hmm, this looks good to me! Triangles have three sides, after all. Let's run the test!

![test failure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcxksmzjwfma0u6fa8r5.png)


Failing? Impossible! Oh wait… Oh no, I made a logical error. If the input of the function is a negative number… That means the result will be a negative number. Let's fix that! Looks like I also forgot to create a new instance of triangle to call methods on.

![fixed method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izqdm5444i9x2vw5suh7.png)



![fixed test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pp6a6dqfc7oh4bzthn6m.png)


Alright… Let's give it a shot!!

![Successful test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhkto58i0t65ohgdjlvx.png)


Yippee!! It passed!! And it easily helped us fix a couple of bugs. Even though its a big work, test-driven development TDD is an incredibly powerful tool. This is just the tip of the iceberg though, there are a lot of different uses and methods for RSpec. If you would like to learn more, here's a link to the documentation. https://rspec.info/documentation/3.11/rspec-core/

I think that the best way to learn RSpec and get familiar with TDD is by incorporating it into your current project whenever you add new features or refactor your code base.

That's it for today everyone! If you got this far thanks for reading!! I'm going to be putting out a lot more blog posts trying to make these seemingly complex topics more beginner friendly, so stay tuned!