CS FundamentalsVery hardProduct-company placement · Python interview

Module report.py does `from pricing import get_rate` and calls get_rate() inside build(). A unit test for build() uses unittest.mock.patch('pricing.get_rate'), but build() still calls the real function. Why?

  1. A. patch works only on classes
  2. B. The mock must be created before pricing is imported anywhere
  3. C. patch never works on functions
  4. D. report already holds its own reference named report.get_rate, so the patch target must be 'report.get_rate' - patch where the name is looked up, not where it is defined

Correct answer: D. report already holds its own reference named report.get_rate, so the patch target must be 'report.get_rate' - patch where the name is looked up, not where it is defined

from-import copies the reference into report's namespace. Replacing pricing.get_rate afterwards does not change report.get_rate. If report had used import pricing and pricing.get_rate(), patching pricing.get_rate would work.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic