Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, apart from importing <code>get_name</code>, you also need to import <code>get_age</code>, since you use it in the Person class. Second, there is nothing like <code>person.get_name</code> defined ever. So you can't patch that. Instead, you probably wanted to patch <code>data_souce.get_name</code>.</p> <p>But unfortunately, that doesn't work, because <code>data_source</code> is a python module, and <code>get_name</code> is a method within it that you want to redefine. The patch decorator would be good if <code>get_name</code> were a method within a <code>DataSource</code> class, for example. I don't know the exact reason why it doesn't work, because I did not take a deep dive into the implemenation details of the mock library, but the intention of the library is different than how you want to use it.</p> <p>To solve the problem, you can eiter turn data_source into a class, or you can temporarily change the meaning of the name <code>get_name</code>, by assigning a different function or a lambda to it:</p> <pre><code>import data_source def test_person(): # replace the function data_source._real_get_name = data_source.get_name data_source.get_name=lamba a: "Bob" # do some work person = Person() name = person.name() # after use of the get_name function, put the original function back data_source.get_name=data_source._real_get_name # assert post condition assert name=="Bob" </code></pre> <p>Of course, changing what functions do severely affects your program and should be done with care, probably only for testing. Make sure you always put the original function back, even if an error is thrown unexpectedly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload