Learn the basics of Rspec helper methods

Tram Ho

Helper methods

While using RSPEC, you must be using a lot of let and let! . There are times when you use let or let! rspec is all working correctly, sometimes let! It’s okay to use let for better performance when it comes to.

You have a good understanding of let and let! yet? If the answer is not yet then this is a reference for you, otherwise leave your views on let and let! Please.

let and let! What is it?

Use let to define a memorized helper method (the value of the method is remembered). The return value of the memorized helper method will be cached between uses of it in the same example, but this is not available between different examples.

Note : let is lazy-evaluated which means it won’t be evaluated until the method it defines is called for the first time.

By default, let is threadsafe, but you can set this through config.threadsafe, which should make let’s performance a bit faster.

let! is similar to let except let! will be executed in a hidden before hook. That means let! will be executed before each example in a group example that declares it.

Difference between let and instance variable

There are two approaches as follows: to define a variable to use with let:

With the before and instance variable:

The difference: With instance variables in the before hook it can be lost from one file to another. With instance variables can be nil, leading to many potential errors.

The let proof creates a method

The code below creates a method my_name and its return value is cached.

Result:

The return value of the method created with let will be cached

The code below calls my_name method twice but prints out “thinking about what my name is…” once and the returned result of my_name is printed 2 times. Why?

Result:

The following code snippet shows us the method my_name is executed once and the return value of that method will be cached. Even if we call the method my_name n times, the result will be retrieved the first time, rather than executing the my_name method n times.

let is lazy evaluation and let! not

let When the method defined by let is called, it is executed. Proof code:

Result:

From the output it is easy to see that until the puts message line of code, the message is executed.

let! True to the definition, let! is similar to let except let! will be executed in a hidden before hook. That means let! will be executed before each example in a group example that declares it.

Proof code:

Result:

Comment: The result of the code shows “let! block is running ” is printed first synonym for let! is executed before each example in the example group that declares it.

For let! is run before each example, so you need to pay attention to the let! to prevent it from running before unnecessary examples. In super big rspec files it will cause very bad performance.

Personally, when you should use let! To create a method that handles creating a new record. Because then we can know clearly and make sure that record was created before using that record in some example.

Conclude

Through the article want to clarify the concept and how to let and let! activity, as well as the differences between them. So that we can write Rspec not only for it to run correctly, but also for top performance.

Reference articles:

  1. https://www.codewithjason.com/difference-let-let-instance-variables-rspec/
  2. https://relishapp.com/rspec/rspec-core/v/3-10/docs/helper-methods/let-and-let
Share the news now

Source : Viblo