Challenge - 5 Problems
RSpec Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested describe and it blocks
What will be the output when running this RSpec test code?
Ruby
RSpec.describe 'Calculator' do describe '#add' do it 'adds two numbers' do expect(2 + 3).to eq(5) end end describe '#subtract' do it 'subtracts two numbers' do expect(5 - 3).to eq(2) end end end
Attempts:
2 left
💡 Hint
Count the number of it blocks inside all describe blocks.
✗ Incorrect
There are two it blocks, each defining one example. Both expectations are correct, so 2 examples run with 0 failures.
🧠 Conceptual
intermediate1:30remaining
Purpose of describe and it blocks in RSpec
What is the main purpose of the
describe and it blocks in RSpec?Attempts:
2 left
💡 Hint
Think about how you organize tests and what runs as a test.
✗ Incorrect
describe is used to group related tests, while it defines each test example that runs an expectation.🔧 Debug
advanced2:00remaining
Identify the error in this RSpec test code
What error will this RSpec code produce when run?
Ruby
RSpec.describe 'Math' do it 'multiplies numbers' do expect(2 * 3).to eq 6 end it 'divides numbers' do expect(10 / 2).to eq(5) end end
Attempts:
2 left
💡 Hint
Check the parentheses in the second it block.
✗ Incorrect
The second it block has a missing closing parenthesis in the expect statement, causing a SyntaxError.
❓ Predict Output
advanced2:00remaining
Output of nested it blocks inside describe
What will be the output of this RSpec code?
Ruby
RSpec.describe 'String' do it 'checks length' do expect('hello'.length).to eq(5) it 'nested it block' do expect('world'.upcase).to eq('WORLD') end end end
Attempts:
2 left
💡 Hint
Consider if RSpec allows it blocks inside other it blocks.
✗ Incorrect
RSpec does not allow nesting it blocks inside other it blocks. Only one example is registered. When run, the inner `it` call raises a runtime error, resulting in 1 example, 1 failure.
🧠 Conceptual
expert2:30remaining
How does RSpec handle multiple describe blocks with the same name?
If you have multiple
describe blocks with the same description string in RSpec, how does RSpec treat them?Attempts:
2 left
💡 Hint
Think about how RSpec organizes tests by describe blocks.
✗ Incorrect
RSpec treats each describe block independently, even if they have the same description string, running all examples in all blocks.