When is using instance variables more advantageous than using let()?

后端 未结 2 1367
悲哀的现实
悲哀的现实 2020-12-16 05:04

There seems to be a lot of support for using let() in rspec to initialize variables. What are some cases for using instance variables (i.e. @name) instead?

2条回答
  •  不思量自难忘°
    2020-12-16 05:35

    I have favored this question, but at the end I am answering to the question myself.

    First to describe the differences

    The let method when used it does not initialize the variable until it is referenced in the test - which makes it different from @instance variables which are initialized from the start.

    In general there is no big diffidence. Use @instance variables when you want be sure that let object is initialized. But on the other hand this can be done the same with let! , so there is no obvious reason for using @instance variable.

    To conclude, to me, there is obvious reason to use @instance variable over let, but there is no obvious reason to use @instance variables over let!

    @instance variables takes more time to load, and by using them, could lead to unwanted complexity in code which could be avoided by using let or let!

    You can take a look at these two answers as well, maybe it will give you some more information:

    When to use RSpec let()?

    RSpec: What is the difference between let and a before block?

提交回复
热议问题