Test variables INSIDE a view function - no return
<p>I'm new to Django + Python so I'm not sure how much of this question will make sense.</p>

<p>Basically, I'm working on a project (not created by me) that has a function within a views.py file. The purpose of this view function is not to give a HTTP response but to send an email. </p>

<p>It accepts a request as a parameter (which contains data that will be looped through - some of it being inserted into the email.</p>

<p>The end of the function does a call to an email_extrals.utils function send_mail_template(various different variables passed in).</p>

<p>My issue is that I want to write a test for this function which essentially returns nothing (that I can see - it just sends an email). This view function is actually called elsewhere in an API. </p>

<p>The purpose of this test is to ensure that if certain variables are present in the request data, then they should be present in a variable which gets passed to the send_email_template() call. So I'm kind of testing for variables inside the function - not what gets returned. </p>

<p>I need a way of somehow accessing the internal variables of the function to test them.</p>

<p>I can't think of a way to test this and at this point I'm banging my head against a wall. Any test info I can find online for views is mainly just testing responses or various model related issues. </p>

<p>Any push in the right direction would be a huge help. A link to docs or a basic example of something similar. </p>

<p>Thanks, 
Dean </p>

<p>One way to approach this would be to make your own fake version of <code>send_email_template()</code> that does nothing except verify that it was called with the desired argument values, and raises an exception if they are wrong.</p>

<p>Then, in your test setup, you would <em>replace</em> the real <code>send_email_template()</code> with your fake one.  This is called <a href="https://docs.python.org/3/library/unittest.mock.html" rel="nofollow">Mocking</a>.</p>

<p>And just to be safe you would also want to assert that your fake function was <em>actually called</em>.</p>

python django testing