RSpec expectations let you check if your code works as you want. Matchers help describe what you expect.
RSpec expectations and matchers in Ruby
expect(actual_value).to matcher(expected_value) expect(actual_value).not_to matcher(expected_value)
expect() wraps the actual value you want to test.
to means you expect the matcher to be true; not_to means you expect it to be false.
expect(5).to eq(5)
expect('hello').to include('ell')
expect([1, 2, 3]).not_to include(4)
expect { raise StandardError }.to raise_error(StandardError)This program tests simple math operations using RSpec expectations and matchers.
It checks addition, array inclusion, and error raising.
require 'rspec/autorun' RSpec.describe 'Simple math' do it 'adds numbers correctly' do expect(2 + 3).to eq(5) end it 'does not equal wrong sum' do expect(2 + 2).not_to eq(5) end it 'includes element in array' do expect([1, 2, 3]).to include(2) end it 'raises error when dividing by zero' do expect { 1 / 0 }.to raise_error(ZeroDivisionError) end end
Matchers like eq, include, and raise_error are common and easy to use.
You can chain not_to to expect something NOT to happen.
RSpec output shows which tests passed or failed, helping you find problems quickly.
RSpec expectations check if your code behaves as expected.
Matchers describe the exact condition you want to test.
Use expect().to for positive checks and expect().not_to for negative checks.
Practice
expect(value).to eq(5) check for?Solution
Step 1: Understand the
Theeqmatchereqmatcher tests for equality between the expected and actual values.Step 2: Analyze the expectation
expect(value).to eq(5)means the test expectsvalueto be exactly 5.Final Answer:
It checks ifvalueis equal to 5. -> Option DQuick Check:
eq matcher = equality check [OK]
- Confusing eq with greater_than or less_than
- Thinking eq checks for inequality
- Mixing up expect().to and expect().not_to
Solution
Step 1: Identify the correct matcher for inclusion
Theincludematcher checks if a collection contains a specific element.Step 2: Match syntax with RSpec standards
expect(array).to include(3)is the correct syntax to check if 3 is in the array.Final Answer:
expect(array).to include(3) -> Option BQuick Check:
include matcher = check element presence [OK]
- Using has or contains which are not valid RSpec matchers
- Using has_key which is for hashes, not arrays
- Forgetting to use expect().to syntax
describe 'Number test' do
it 'checks if number is odd' do
number = 7
expect(number.odd?).to be true
end
endSolution
Step 1: Understand the code logic
The variablenumberis set to 7, which is an odd number.Step 2: Analyze the expectation
expect(number.odd?).to be truechecks ifnumber.odd?returns true, which it does for 7.Final Answer:
The test passes because 7 is odd. -> Option AQuick Check:
odd? returns true for odd numbers [OK]
- Assuming 7 is even
- Confusing be true with eq true
- Thinking syntax is wrong here
expect(user.name).to eq nil
Solution
Step 1: Understand nil checking in RSpec
Whileeq nilworks, the preferred matcher for nil isbe_nilfor clarity and style.Step 2: Identify best practice
Usingexpect(user.name).to be_nilis clearer and idiomatic in RSpec.Final Answer:
Should usebe_nilmatcher instead ofeq nil. -> Option CQuick Check:
Use be_nil for nil checks [OK]
- Thinking eq nil is a syntax error
- Adding unnecessary parentheses
- Using not_to eq nil to check for nil
calculate returns a value greater than 10 but less than 20. Which RSpec expectation correctly checks this?Solution
Step 1: Understand the range requirement
The value must be greater than 10 and less than 20, excluding 10 and 20 themselves.Step 2: Analyze each option
expect(calculate).to be > 10 and be < 20:expect(calculate).to be > 10 and be < 20is invalid syntax; correct chaining uses(be > 10).and(be < 20).
Options C and D:be_betweenis not a built-in RSpec matcher.
expect(calculate).to satisfy { |v| v > 10 && v < 20 }:expect(calculate).to satisfy { |v| v > 10 && v < 20 }correctly checks the condition using a standard flexible matcher.Step 3: Choose the best matcher
expect(calculate).to satisfy { |v| v > 10 && v < 20 } is the correct and explicit choice using standard RSpec.Final Answer:
expect(calculate).to satisfy { |v| v > 10 && v < 20 } -> Option AQuick Check:
satisfy matcher = custom condition check [OK]
- Using non-standard be_between matcher
- Trying to chain matchers with 'and' incorrectly
- Ignoring boundary conditions in range checks
Ready to prove it?
Pass the test and this pattern moves unknown
