0
0
Rubyprogramming~10 mins

Mocking and stubbing in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a mock object in Ruby.

Ruby
mock_user = double([1])
Drag options to blanks, or click blank then click option'
A:user
B"User"
CUser.new
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a symbol.
Trying to instantiate a real object instead of a mock.
2fill in blank
medium

Complete the code to stub the method 'name' to return 'Alice'.

Ruby
allow(mock_user).to receive(:name).and_return([1])
Drag options to blanks, or click blank then click option'
A"Alice"
Bname
C:Alice
DAlice
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol instead of a string.
Not quoting the return value.
3fill in blank
hard

Fix the error in the code to expect the method 'greet' to be called once.

Ruby
expect(mock_user).to [1](:greet).once
Drag options to blanks, or click blank then click option'
Areceive_message
Bcall
Cexpect_message
Dreceive
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect matcher names.
Omitting the receive keyword.
4fill in blank
hard

Fill both blanks to stub a method 'age' that returns 30 only if called with argument 'true'.

Ruby
allow(mock_user).to receive(:age).with([1]).and_return([2])
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C30
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Using false instead of true.
Returning the wrong number.
5fill in blank
hard

Fill all three blanks to create a mock, stub 'status' to 'active', and expect 'update' to be called once.

Ruby
mock_account = double([1])
allow(mock_account).to receive(:status).and_return([2])
expect(mock_account).to receive([3]).once
Drag options to blanks, or click blank then click option'
A:account
B"active"
C:update
D:status
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong symbols or strings for mock name or return value.
Expecting the wrong method to be called.