From the course: Ruby: Testing with RSpec

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Setting a subject

Setting a subject - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Setting a subject

- In this movie we're going to learn how to set a subject in your RSpec example groups. In the last movie we learned about how to use let, and let defines a helper method using the name that we give it. So in this case, we have a helper method named car, it will then call the block of code assigned after it, in this case Car.new. Imagine that we change the name of that let helper method from being car to being subject. It would look like this. This code would do exactly the same thing. All we've done is rename the method. Now it's so common for our examples to be referencing a single instance variable, the subject of the example, that RSpec has a shorthand for it. That is to just say subject. It's exactly the same as what we saw in the last slide. We just simply write subject, and that's the same thing as saying let subject be a new helper method with this block of code. Now we not only create the same instance method, but we've also made it clear in our code that the focus is on this…

Contents