<img height="1" width="1" style="display:none;" alt="" src="https://ct.pinterest.com/v3/?tid=2613694256380&amp;pd[em]=<hashed_email_address>&amp;noscript=1">
Get Started

Taste of Test

Josh Hnath, Jun, 12 2017 | 1 min read


Writing a test in Ruby can come in two popular flavors, minitest and Rspec. When I was at my first ever professional gig we were using minitest, and where I’m at now we use Rspec. Minitest is the default testing library for Ruby, but in my opinion, Rspec is preferred because it has more gems and better build tools. Whichever way you decide to go however, you’ll be able to sufficiently test your Ruby code. Below is an example of both minitest and Rspec in action.

First we will look at minitest. Say we have a Teacher class with a method called "ask" which accepts a question as an argument, the method always returns “I’m not sure”. To write a test for this we have a class called TeacherTest which inherits from our minitest library. We call our test test_ask_returns_an_answer, we set teacher equal to a new instance of our Teacher class and we use the keyword assert (which comes from our minitest library) teacher.ask (here we invoke the method we want to test) == “I’m not sure”. So we are calling our method and testing that the returned response is equal to what we would expect from our method.

Screen Shot 2017-06-12 at 1.36.40 PM.png

If we wanted to write this same test in Rspec it would look like this. We describe our class, in this case Teacher. The keyword ‘subject’ refers to our class, so in turn we expect Teacher.new.ask to equal “I’m not sure.”

Screen Shot 2017-06-12 at 1.36.54 PM.png

You might also like ...

8 Tips to Reading Documentation: A Newbie’s Guide in 2021

05/2021 Have you ever been on the verge of tearing your hair out trying to figure out why a line of code is failing? It’s time to tur...

Michele Barard   |   3 min read

Read More

Technical Assessments 101: What Are They, And Why Do They Matter?

Technical assessments are a great way for hiring managers to find qualified candidates, and studies have shown that people who are hi...

Olivia Jones   |   4 min read

Read More

Pros and Cons of Being a Tech Consultant

Here’s what you need to know about the pros and cons of working as a tech consultant for a technology consultant firm.

Olivia Jones   |   5 min read

Read More