The difference between let, let! and instance variables in RSpec - Code with Jason

First, there’s the stylistic difference. The syntax is of course a little different between the two approaches. Instance variables are of course prefixed with @. Some people might prefer one syntax over the other. I personally find the let syntax ever so slightly tidier.There are also a couple mechanical differences. Because of how instance variables work in Ruby, you can use an undefined instance variable and Ruby won’t complain. This presents a slight danger. You could for example accidentally pass some undefined instance variable to a method, meaning you’d really be passing nil as the argument. This means you might be testing something other than the behavior you meant to test. This danger is admittedly remote though. Nonetheless, the let helper defined not an instance variable but a new method (which we’ll see in detail shortly), meaning that if you typo your method’s name, Ruby will most certainly complain, which is of course good.
The difference between let, let! and instance variables in RSpec - Code with Jason #ruby #rubydeveloper #rubyonrails #code #rspec https://rubyonrails.ba/single/the-difference-between-let-let-and-instance-variables-in-rspec-code-with-jason

Nezir Zahirovic

Contractor Ruby On Rails (8+ years) / MCPD .Net / C# / Asp.Net / CSS / SQL / (11 years)

related articles