From the course: Ruby: Testing with RSpec

What is RSpec? - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

What is RSpec?

- Let's begin by introducing RSpec. What is it? RSpec is a testing framework, so we first need to understand what a test is. If you're a developer, then you've already performed software tests, whether you realize it or not. You write a bit of code, and then you try it to see if it works. That's testing. You might create a web form and then submit the form to see if it successfully adds the data to the database. That's testing. You submit the same form again, but this time using bad data to see if your code rejects the form and tells the user about the problem. That's testing, but it's manual testing, and it can be tedious and repetitive. Every time you make a change to your code, you have to test it again, manually. But you know what's great at performing repetitive tasks? Computers. So when we talk about testing, we're talking about writing code than can perform tests on other code. RSpec is a framework that allows us to do that. The "R" stands for Ruby, and "Spec" is short for Specification. A specification is a detailed requirement that our code should meet. Or more formally, it's an executable example that tests whether a portion of code exhibits the expected behavior in a controlled context. We'll elaborate on that in just a moment. A spec can also be referred to as an example or as a test, and they're used fairly interchangeably. There's a basic structure to software tests that RSpec follows. Given some context, when some event occurs, then I expect some outcome, and that was what was alluded to in that formal definition. Given this controlled context, if we do something, we want to test whether we get the right outcome. Given... When... and Then... And it's the basic structure of all tests, so you want to make sure that you're familiar with that. In our previous example, given that we have a web form, if we fill out that form with valid data and submit it, then I expect that the outcome is that it will be submitted to the database. Given that web form, when I fill out the form with bad data and submit it, then I expect that the outcome is that the form will not be submitted. It will be rejected and the user will be notified about the problems on the form. We're just going to be formalizing this Given... When... Then... process in our code. RSpec is written in Ruby. That's what the "R" stands for. But it uses a domain-specific language, or DSL for short. That's a fancy way of saying that RSpec has its own way of doing things, that we have to talk to RSpec in its own language, not just in Ruby. Sometimes you'll even find that it's not very Ruby-like in the way that it does things. But you will use Ruby a lot, and you need to be proficient in Ruby. Make sure that you learn Ruby first, and then you'll be able to learn that DSL layer that RSpec adds on top of Ruby. If you need to learn more about Ruby, you can take my Ruby Essential Training course to get a good primer. We should also discuss how RSpec fits with the popular web framework Ruby on Rails, because Ruby on Rails is the most common use there is for RSpec. Most people who are using RSpec are using it on Rails applications. But Rails is not required for RSpec. Don't be deceived into thinking that they have to go hand in hand. RSpec can test basic Ruby code too. So my recommendation and the path that this course is going to follow is we're first going to use RSpec with basic Ruby, and then we'll be able to use RSpec with Rails as well. The reason why is that Rails has a lot of complexity that we don't want to have to deal with here. We want to focus on RSpec, so we want to keep the target of our tests as simple as possible so that we can focus on the testing framework itself. Now that we know what RSpec is, I want to make sure that you understand why it's so important that we write software tests.

Contents