0
0
Rubyprogramming~5 mins

RSpec expectations and matchers in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of expect() in RSpec?

expect() is used to define an expectation in RSpec. It wraps the actual value you want to test, so you can check if it meets certain conditions using matchers.

Click to reveal answer
beginner
What does the matcher eq check for in RSpec?

The eq matcher checks if the actual value is equal to the expected value using Ruby's == operator.

Click to reveal answer
intermediate
How do you test if a method raises an error using RSpec?

You use expect { ... }.to raise_error(ErrorClass). The block contains the code that should raise the error, and raise_error matcher checks for it.

Click to reveal answer
intermediate
What is the difference between eq and eql matchers?

eq checks for value equality (==), while eql checks for both value and type equality (eql? method).

Click to reveal answer
beginner
How can you check if an array includes a specific element in RSpec?

Use expect(array).to include(element). The include matcher tests if the element is present in the array.

Click to reveal answer
Which RSpec matcher checks if two values are exactly the same object?
Aeq
Beql
Cequal
Dinclude
How do you write an expectation that a method call changes a value by 1?
Aexpect(value).to change.by(1)
Bexpect(method_call).to eq(1)
Cexpect { method_call }.to raise_error(1)
Dexpect { method_call }.to change { value }.by(1)
What does expect(value).not_to eq(5) mean?
AValue should not be equal to 5
BValue should be equal to 5
CValue should raise an error 5 times
DValue should include 5
Which matcher would you use to check if a string matches a pattern?
Amatch
Binclude
Ceq
Draise_error
How do you test if a block of code raises any error in RSpec?
Aexpect(code).to raise_error
Bexpect { code }.to raise_error
Cexpect { code }.to eq(error)
Dexpect(code).to eq(error)
Explain how to use RSpec expectations and matchers to test if a method returns the expected value.
Think about wrapping the method call with expect and using a matcher to compare.
You got /4 concepts.
    Describe how to test error handling in RSpec using expectations and matchers.
    Remember that error testing uses a block inside expect.
    You got /4 concepts.