Command Palette

Search for a command to run...

Hectal
Phase 11: Testing and Code Quality
Python · Exercise 4 of 6 PreviousNext
Intermediate

Patch a method so the test does not need the network

RateService.rate(currency) pretends to call the internet and raises RuntimeError. to_dollars(rupees, service) divides rupees by the rate.

Use unittest.mock.patch.object in a with block so that RateService.rate returns 80.0 inside the block. Inside the block call to_dollars(800, RateService()) and store the answer in result. Then print Dollars: followed by result, for example Dollars: 10.0.

After the with block, the original method must be back, so calling RateService().rate("USD") raises RuntimeError again.

What Check tests

  1. result is 10.0
  2. The program prints Dollars: 10.0
  3. The original method is restored after the block
  4. The code uses patch.object and a with block

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.