title: Django : How can I mock request.session of view?
body: <p>I'm using mocking to test views.</p>
  
  <p><strong>tests.py</strong></p>
  
  <pre><code>@patch('orders.views.OrderView.generate_merchant_uid')
  def test_expected_price_is_registered_on_GET_request(self, mock_generate_merchant_uid):
      self.client.get(reverse('orders:order'))
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>class OrderView(LoginRequiredMixin, View):
  
      def generate_merchant_uid(self):
          merchant_uid = "blah_blah_blah"
          return merchant_uid
  
      def get(self, request, *args, **kwargs):
          merchant_uid = self.generate_merchant_uid()
          request.session['merchant_uid'] = merchant_uid
          return HttpResponse('a')
  </code></pre>
  
  <p>It occurs errors:</p>
  
  <pre><code>TypeError: &lt;MagicMock name='generate_merchant_uid()' id='4431843456'&gt; is not JSON serializable
  </code></pre>
  
  <p>It occurs error because I mocked <code>generate_merchant_uid</code> and it returns <code>MagicMock</code> and <code>View</code> trying to store this <code>MagicMock</code> in the <code>request.session</code>.</p>
  
  <p>I think what I have to do is to mock <code>request.session</code>. </p>
  
  <p>But have no idea how I can do that.</p>
  
  <p>Need advices. Thanks.</p>