From the course: Ruby: Testing with RSpec

Unlock the full course today

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

Using the let method

Using the let method - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Using the let method

- In this movie, we're going to learn to use RSpec's Let Method to help make our tests more efficient. In the previous movie, we took a look at before hooks, and before hooks are very useful for setup inside your example groups. But if you're just setting up variables, then you should consider using RSpec's Let Method instead. Let's review the before filter that we wrote in the last movie. We had before(:example), and inside a code block, we had the code that would be performed before each example. In this case we're creating an instance of the car class and assigning it to the instance variable, @car. Then in our example, we're able to call expect (@car.wheels).to eq(4). There are a couple of problems with this. First, it gets called before every example. That's what we told it to do. If we have 10 examples in the group, and one of those examples never even calls @car, it doesn't matter. The code is going to execute, and the instance variable is going to be assigned anyway. In this…

Contents