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?✗ Incorrect
let creates a memoized helper method that is evaluated only when called in a test.
When is the code inside a
before hook executed?✗ Incorrect
before hooks run their code before every individual test example.
Which is true about
let variables in RSpec?✗ Incorrect
let variables are lazily evaluated and cached for each test example.
What happens if you call a
let variable inside a before hook?✗ Incorrect
Calling a let variable inside a before hook evaluates it then and memoizes the value.
Why might you prefer
let over instance variables for test setup?✗ Incorrect
let variables are lazily evaluated and scoped to each test, avoiding unnecessary setup and state leaks.
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.