0
0
Rubyprogramming~5 mins

Let and before hooks in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the let method in Ruby testing?
The let method defines a memoized helper method. It lazily creates and caches a value the first time it is called in a test, so the same value is reused within that example.
Click to reveal answer
beginner
How does a before hook work in Ruby testing frameworks like RSpec?
A before hook runs a block of code before each example (test). It is used to set up common test data or state needed for the tests.
Click to reveal answer
intermediate
What is the difference between let and before hooks?
let defines a lazily evaluated value that is memoized per example, while before hooks run code eagerly before each example, often for setup tasks.
Click to reveal answer
intermediate
Can let variables be accessed inside before hooks?
Yes, but the let variable is not evaluated until it is called. If you call it inside a before hook, it will be created at that time.
Click to reveal answer
intermediate
Why is it better to use let instead of instance variables in tests?
let helps avoid unnecessary setup by creating values only when needed, making tests faster and cleaner. It also scopes the value to each example, avoiding state leaks.
Click to reveal answer
What does the let method do in Ruby tests?
ADefines a memoized helper method evaluated lazily
BRuns code before each test example
CDefines a global variable
DRuns code after each test example
When is the code inside a before hook executed?
AOnly when a <code>let</code> variable is called
BAfter each test example
COnly once before all tests
DBefore each test example
Which is true about let variables in RSpec?
AThey are evaluated lazily and cached per example
BThey are evaluated immediately when defined
CThey run before all tests
DThey cannot be used inside <code>before</code> hooks
What happens if you call a let variable inside a before hook?
AIt raises an error
BIt is ignored
CIt is evaluated at that time and memoized
DIt runs after the test example
Why might you prefer let over instance variables for test setup?
ABecause <code>let</code> variables are global
BBecause <code>let</code> variables are lazily evaluated and scoped per example
CBecause instance variables are faster
DBecause <code>let</code> variables run after tests
Explain how let and before hooks differ in their execution and purpose in Ruby tests.
Think about when the code runs and what it is used for.
You got /4 concepts.
    Describe a scenario where using let is better than using instance variables in test setup.
    Consider test speed and isolation.
    You got /4 concepts.