<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Dan,<br>
<br>
<div class="moz-cite-prefix">Le 03/11/2016 à 10:35, Daniel Bradburn
a écrit :<br>
</div>
<blockquote
cite="mid:CAO-Qhwt4pL+APsZ5KgbUdRsNV0STNwXWqJfiG_Lgud_fPzTSgA@mail.gmail.com"
type="cite">
<div>Ah I see what you are trying to do now, I think the problem
is that your function is returning <span
style="color:rgb(0,0,0);font-family:consolas">r.text</span> which
is a new mock object when really you want to assert attribute
access on the original return value from requests, this seems to
reflect your use case:<br>
</div>
<div>
<pre style="color:rgb(0,0,0);font-family:consolas">>>> <span style="color:rgb(0,0,128);font-weight:bold">import </span>mock
>>> r = mock.MagicMock()
>>> r = r.text
>>> <span style="color:rgb(0,0,128);font-weight:bold">assert </span><span style="color:rgb(0,128,0);font-weight:bold">'text' </span><span style="color:rgb(0,0,128);font-weight:bold">in </span>r._mock_parent._mock_children</pre>
</div>
<div>However, it is probably unwise to access the protected
attributes like this, perhaps you could just mock <span
style="color:rgb(0,0,0);font-family:consolas">requests.get(call_url,
</span><span style="font-family:consolas;color:rgb(102,0,153)">headers</span><span
style="color:rgb(0,0,0);font-family:consolas">=headers) </span>yourself,
I think you can probably use something like this as a starting
point for what you are trying to do...</div>
</blockquote>
<br>
Thanks Dan, that's it.<br>
<br>
And you're also right: it's ugly.<br>
<br>
I was hoping to find a quick way to do it in a short time. <br>
It is interesting, and I will develop something around that.<br>
<br>
Nice colored formating, how do you did that?<br>
<blockquote type="cite">
<div>
<pre style="color:rgb(0,0,0);font-family:consolas">r = mock.MagicMock()
<span style="color:rgb(0,0,128);font-weight:bold">with </span>mock.patch(<span style="color:rgb(0,128,0);font-weight:bold">'requests.get'</span>, <span style="color:rgb(102,0,153)">return_value</span>=r) <span style="color:rgb(0,0,128);font-weight:bold">as </span>get:
result = function_under_test(<span style="color:rgb(0,128,0);font-weight:bold">'<a href="https://www.google.nl">https://www.google.nl</a>'</span>, <span style="color:rgb(0,0,128)">False</span>)
<span style="color:rgb(0,0,128)">str</span>(result)
r.json.assert_called_once()
r.text.<span style="color:rgb(178,0,178)">__str__</span>.assert_not_called()
</pre>
</div>
</blockquote>
<br>
I still not "like" that way either.<br>
<br>
What is the effect of: <span style="color:rgb(0,0,128)">str</span>(result)
?<br>
<br>
Regards,<br>
Sylvain.<br>
<pre class="moz-signature" cols="72">--
Sylvain Viart - DevOps système linux - freelance developer</pre>
</body>
</html>