0
0
Rubyprogramming~20 mins

RSpec describe and it blocks in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RSpec Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A2 examples, 0 failures
B1 example, 0 failures
C2 examples, 2 failures
D0 examples, 0 failures
Attempts:
2 left
💡 Hint
Count the number of it blocks inside all describe blocks.
🧠 Conceptual
intermediate
1:30remaining
Purpose of describe and it blocks in RSpec
What is the main purpose of the describe and it blocks in RSpec?
A<code>describe</code> defines variables; <code>it</code> groups tests
B<code>describe</code> runs tests; <code>it</code> sets up variables
C<code>describe</code> groups tests; <code>it</code> defines individual test examples
D<code>describe</code> is for assertions; <code>it</code> is for setup
Attempts:
2 left
💡 Hint
Think about how you organize tests and what runs as a test.
🔧 Debug
advanced
2: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
ASyntaxError due to missing closing parenthesis
BNo error, tests pass
CNameError for undefined method
DRuntimeError due to division by zero
Attempts:
2 left
💡 Hint
Check the parentheses in the second it block.
Predict Output
advanced
2: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
ASyntaxError: nested 'it' blocks are not allowed
B2 examples, 0 failures
C0 examples, 0 failures
D1 example, 1 failure
Attempts:
2 left
💡 Hint
Consider if RSpec allows it blocks inside other it blocks.
🧠 Conceptual
expert
2: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?
ARSpec merges them into one group and runs examples once
BRSpec treats them as separate groups and runs all their examples
CRSpec runs only the first describe block and ignores others
DRSpec raises an error about duplicate describe blocks
Attempts:
2 left
💡 Hint
Think about how RSpec organizes tests by describe blocks.