Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a mock object in Ruby.
Ruby
mock_user = double([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a symbol.
Trying to instantiate a real object instead of a mock.
✗ Incorrect
In Ruby, double(:user) creates a mock object named :user.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol instead of a string.
Not quoting the return value.
✗ Incorrect
Use a string "Alice" to return the name properly.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect matcher names.
Omitting the
receive keyword.✗ Incorrect
The correct syntax is expect(mock_user).to receive(:greet).once.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
false instead of true.Returning the wrong number.
✗ Incorrect
The method age is stubbed to return 30 when called with true.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong symbols or strings for mock name or return value.
Expecting the wrong method to be called.
✗ Incorrect
This code creates a mock named :account, stubs status to return "active", and expects update to be called once.