DEV Community

Aliya Lewis
Aliya Lewis

Posted on • Updated on

Testing the Waters with RSpec (Part 1)

If you've read my last blog post, Test-Driven Development, then you know that lately, I've been digging deeper into how to write tests. This past week I was working on a technical assessment for a job I applied for and I had to make a CLI application using the Google Books API. That sounded simple enough since I had done a couple of CLI apps in my boot camp in Ruby.

When I decided to do this technical assessment in Ruby, I was feeling pretty confident about it, until I actually sat down and tried to do it! I realized I hadn't touched anything in Ruby-land for months and suddenly became very anxious about how this project was going to go. After a shaky start, I found some great resources (that I'll share at the end of this post) that helped me become more at ease.


So the first thing you'll need to do is make sure that you install 'rspec' and 'rspec-expectations'.

Alt Text

Then, run 'rspec --init' to create the spec folders/files for your project.

The Test

There's some basic syntax that comes with RSpec which is really easy and a great way to start learning about testing. Take a look at the screenshot below and I'll go into more detail about it.

Alt Text

As you can see, I created a file called 'mytest_spec.rb' where I've required json and rest-client at the top of the file. This is so that when I make a request to the specified endpoint, my app will be able read and parse the information it receives. Line 4 defines the which type of test is going to run, is it for a controller? Model?

'context' is optional for test writing but I find it to be a nice way to group tests together. The next part, 'it' is what will show up in the terminal when you run the tests, so whatever you are expecting, write a sentence about it there. I didn't use 'context' for this project but if you're interested in seeing it used, I'll provide an example towards the end of this blog.

I'm testing if the response I get from the Google Books API is good and doesn't send back an error so on line 11, I define what I'm expecting to happen once a call is made to the API.

Since I'm testing my user model validations, I wrote some tests specifying the behavior of what I expect to see in certain cases. The first test checks if there is a name, the second test checks if there is an email and the third test checks that both are present. These are simple tests and I'm working towards learning to write more complex things.

Run the Test

To run a test, type 'rspec spec/testing/mytest_spec.rb' or whatever your file path is into your terminal. You should see something like this:

Alt Text

Yay! The first test passed! I'm still figuring out some things will RSpec (and for this app, how to use it without Rails) so I hope that in the next blog I'll be able to show you more complex tests.

I have written some tests in RSpec for a Rails app, here's an example if you're interested in checking it out.

Alt Text

That's all I have for now, but I'll be back next week!

If you have any tips/tricks or know of any good resources for test writing, please let me know in the comments. :)


Top comments (0)