擅长:python、mysql、java
<p>首先,您可以并且应该模仿<code>xmlrpc.client.ServerProxy</code>;您的库将<code>xmlrpc.client</code>作为一个新名称导入,但它仍然是相同的模块对象,因此库中的<code>xmlrpclib.ServerProxy</code>和{<cd1>}都会导致相同的对象。在</p>
<p>接下来,看看对象是如何使用的,并查找<em>calls</em>,即<code>(..)</code>语法。库使用的服务器代理如下所示:</p>
<pre><code># a call to create an instance
auth_server = xmlrpclib.ServerProxy(AuthURL)
# on the instance, a call to another method
valid = auth_server.isTokenValid(token)
</code></pre>
<p>因此这里有一个链,在这里调用mock,然后使用返回值来查找另一个也被调用的属性。在模拟时,您需要寻找相同的链;为此使用<a href="https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.return_value" rel="noreferrer">^{<cd6>} attribute</a>。默认情况下,调用mock时会返回一个新的mock实例,但您也可以设置测试值。在</p>
<p>所以要测试代码,您需要影响<code>auth_server.isTokenValid(token)</code>返回的内容,并测试代码是否正常工作。您可能还想断言正确的URL被传递到<code>ServerProxy</code>实例。在</p>
<p>为不同的结果创建单独的测试。可能令牌在一种情况下有效,而在另一种情况下无效,您需要测试这两种情况:</p>
^{pr2}$