0
0
Rubyprogramming~5 mins

RSpec describe and it blocks in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the describe block in RSpec?
The <code>describe</code> block groups related tests together. It usually wraps tests for a specific class or method, helping organize and explain what is being tested.
Click to reveal answer
beginner
What does an it block represent in RSpec?
An it block defines a single example or test case. It describes a specific behavior or expectation that should be true.
Click to reveal answer
beginner
How do describe and it blocks work together?
The describe block groups tests, and inside it, multiple it blocks define individual tests. This structure makes tests clear and organized.
Click to reveal answer
beginner
Why should it block descriptions be written in plain language?
Writing it block descriptions in plain language helps anyone reading the tests understand what behavior is expected, making tests like documentation.
Click to reveal answer
beginner
Example: What does this RSpec code test?<br>
describe 'Calculator' do
  it 'adds two numbers' do
    expect(1 + 2).to eq(3)
  end
end
This code tests that adding 1 and 2 results in 3. The describe block groups tests for 'Calculator', and the it block checks the addition behavior.
Click to reveal answer
What is the main role of the describe block in RSpec?
ATo group related tests
BTo run a test
CTo define a variable
DTo skip a test
Which block in RSpec defines a single test case?
Ait
Bcontext
Cdescribe
Dbefore
Inside which block do you place multiple it blocks?
Ait
Bdescribe
Cexpect
Dlet
What should the description inside an it block explain?
AThe test framework version
BThe file location
CThe code implementation details
DThe expected behavior being tested
What does this code check?<br>
describe 'Array' do
  it 'is empty when created' do
    expect([].empty?).to be true
  end
end
AThat an array has elements when created
BThat an array is nil
CThat an array is empty when created
DThat an array raises an error
Explain how describe and it blocks help organize tests in RSpec.
Think about grouping and individual checks.
You got /4 concepts.
    Write a simple RSpec test using describe and it blocks to check if 2 + 2 equals 4.
    Use expect(2 + 2).to eq(4) inside it.
    You got /3 concepts.