0
0
Rubyprogramming~10 mins

Test doubles concept 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 test double named 'user'.

Ruby
user = [1]("user")
Drag options to blanks, or click blank then click option'
Amock
Bdouble
Cstub
Dfake
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mock' instead of 'double' which is not a direct method in RSpec.
Confusing 'stub' which is used to replace methods, not create doubles.
2fill in blank
medium

Complete the code to make the test double respond to 'name' with 'Alice'.

Ruby
allow(user).to receive(:[1]).and_return("Alice")
Drag options to blanks, or click blank then click option'
Aage
Bid
Cname
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using an unrelated method name that the double does not respond to.
Confusing the symbol name with a string.
3fill in blank
hard

Fix the error in the code to verify the double received the 'save' message.

Ruby
expect(user).to have_received(:[1])
Drag options to blanks, or click blank then click option'
Acreate
Bdelete
Cupdate
Dsave
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that was never called on the double.
Confusing the matcher syntax.
4fill in blank
hard

Fill both blanks to create a double that responds to 'title' with 'Book' and 'author' with 'John'.

Ruby
book = double("book", [1]: "Book", [2]: "John")
Drag options to blanks, or click blank then click option'
Atitle
Bname
Cauthor
Dpublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated attribute names.
Mixing up keys and values.
5fill in blank
hard

Fill all three blanks to create a double 'car' with 'make' as 'Toyota', 'model' as 'Corolla', and verify it received 'start'.

Ruby
car = double("car", [1]: "Toyota", [2]: "Corolla")
allow(car).to receive(:start)
car.start
expect(car).to have_received(:[3])
Drag options to blanks, or click blank then click option'
Amake
Bmodel
Cstart
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong attribute names.
Checking for a method that was not called.