<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 2020

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 turn to doc...

Michele Barard   |   2 min read

Read More

17 Data Scientists of Color That You Should Follow Right Now

We’ve curated a list of people of color who are providing valuable insights, wisdom, and projects in the fields of data science, AI, ...

Sydney Stern Miller   |   7 min read

Read More

Tech Talent South takes their Graduate Accelerator Online, Free to College Juniors & Seniors whose Campuses are Shut Down

In response to campus shutdowns due to COVID-19, Tech Talent South is offering a free full-stack course to college juniors and senior...

Meagan Michal   |   < 1 min read

Read More