Challenge - 5 Problems
RSpec Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
The include matcher checks if the given element is present in the collection.
✗ Incorrect
The include matcher in RSpec checks if the expected element is present in the array. Since 3 is in [1, 2, 3, 4], the test passes.
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Check if have_length is a valid RSpec matcher.
✗ Incorrect
RSpec does not have a built-in matcher called have_length. The correct matcher is have_attributes(length: 5) or use expect(string.length).to eq(5).
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Use a block with expect to test for errors.
✗ Incorrect
To test for errors in RSpec, you must pass a block to expect. Options B and D call the expression immediately, causing the error before expect runs. Option B uses a wrong matcher name.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
Consider variable scope inside RSpec blocks.
✗ Incorrect
The 'result' inside the it block is a different local variable than the one outside. The outer 'result' remains nil after the test runs.
🔧 Debug
expert2: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
Attempts:
2 left
💡 Hint
Check the difference between 'be true' and 'be_truthy' matchers.
✗ Incorrect
'be true' matcher expects the exact object true (i.e., object identity). arr.empty? returns true (boolean), so it should pass. However, in some Ruby versions or RSpec configurations, subtle differences cause failures. The recommended matcher is 'be_truthy' which checks truthiness instead of object identity.