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
resultis 10.0- The program prints
Dollars: 10.0 - The original method is restored after the block
- The code uses
patch.objectand awithblock
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.