0
0
Rubyprogramming~20 mins

RSpec expectations and matchers 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
What is the output of this RSpec expectation?
Consider the following RSpec test snippet. What will be the result when it runs?
Ruby
describe 'Array' do
  it 'checks if array includes 3' do
    expect([1, 2, 3, 4]).to include(3)
  end
end
AThe test passes because 3 is included in the array.
BThe test fails because include matcher only works with strings.
CThe test raises a SyntaxError due to missing parentheses.
DThe test fails because the array does not include 3.
Attempts:
2 left
💡 Hint
The include matcher checks if the given element is present in the collection.
Predict Output
intermediate
2:00remaining
What error does this RSpec expectation raise?
What error will this RSpec test produce when run?
Ruby
describe 'String' do
  it 'checks string length' do
    expect('hello').to have_length(5)
  end
end
ANo error, test passes because 'hello' length is 5.
BSyntaxError due to incorrect matcher syntax.
CNoMethodError because have_length matcher does not exist.
DTest fails with expectation failure because length is not 5.
Attempts:
2 left
💡 Hint
Check if have_length is a valid RSpec matcher.
🧠 Conceptual
advanced
2:00remaining
Which matcher correctly tests for raising an error?
You want to test that a method call raises a ZeroDivisionError. Which RSpec expectation is correct?
Aexpect(1 / 0).to raise_error(ZeroDivisionError)
Bexpect { 1 / 0 }.to raise_error(ZeroDivisionError)
Cexpect { 1 / 0 }.to raise_exception('ZeroDivisionError')
Dexpect(1 / 0).to raise_exception(ZeroDivisionError)
Attempts:
2 left
💡 Hint
Use a block with expect to test for errors.
Predict Output
advanced
2:00remaining
What is the value of variable 'result' after this RSpec test?
Given this RSpec test, what will be the value of 'result' after it runs?
Ruby
result = nil
RSpec.describe 'Test' do
  it 'sets result' do
    result = 10
    expect(result).to eq(10)
  end
end
Aresult is nil
Bresult is 10
Cresult is undefined and raises NameError
Dresult is 0
Attempts:
2 left
💡 Hint
Consider variable scope inside RSpec blocks.
🔧 Debug
expert
2:00remaining
Why does this RSpec test fail unexpectedly?
This test is supposed to check if an array is empty, but it fails. Why?
Ruby
describe 'Array' do
  it 'is empty' do
    arr = []
    expect(arr.empty?).to be true
  end
end
AThe test fails because 'be true' matcher is deprecated and should be replaced with 'be_truthy'.
BThe test fails because 'arr.empty?' returns false for an empty array.
CThe test fails because 'expect' requires a block when testing boolean values.
DThe matcher 'be true' expects the exact object 'true', but arr.empty? returns a boolean value, causing failure.
Attempts:
2 left
💡 Hint
Check the difference between 'be true' and 'be_truthy' matchers.