10

So, I've just started using mock with a Django project. I'm trying to mock out part of a view which makes a request to a remote API to confirm a subscription request was genuine (a form of verification as per the spec I'm working to).

What I have resembles:

class SubscriptionView(View): def post(self, request, **kwargs): remote_url = request.POST.get('remote_url') if remote_url: response = requests.get(remote_url, params={'verify': 'hello'}) if response.status_code != 200: return HttpResponse('Verification of request failed') 

What I now want to do is to use mock to mock out the requests.get call to change the response, but I can't work out how to do this for the patch decorator. I'd thought you do something like:

@patch(requests.get) def test_response_verify(self): # make a call to the view using self.app.post (WebTest), # requests.get makes a suitable fake response from the mock object 

How do I achieve this?

3
  • Deadset on using mocks? There is also django.test.client.RequestFactory - docs.djangoproject.com/en/1.5/topics/testing/advanced/… Commented Apr 17, 2013 at 20:29
  • 3
    Just for future viewers, the questioner wanted to mock out an external API call. Not the call to the view itself. Mocks seem very sensible in that situation. Commented Jun 6, 2014 at 0:29
  • As per @aychedee this is indeed what I was aiming for with this question. Commented Jun 7, 2014 at 9:14

2 Answers 2

13

You're almost there. You're just calling it slightly incorrectly.

from mock import call, patch @patch('my_app.views.requests') def test_response_verify(self, mock_requests): # We setup the mock, this may look like magic but it works, return_value is # a special attribute on a mock, it is what is returned when it is called # So this is saying we want the return value of requests.get to have an # status code attribute of 200 mock_requests.get.return_value.status_code = 200 # Here we make the call to the view response = SubscriptionView().post(request, {'remote_url': 'some_url'}) self.assertEqual( mock_requests.get.call_args_list, [call('some_url', params={'verify': 'hello'})] ) 

You can also test that the response is the correct type and has the right content.

Sign up to request clarification or add additional context in comments.

Comments

3

It's all in the documentation:

patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)

target should be a string in the form ‘package.module.ClassName’.

from mock import patch # or @patch('requests.get') @patch.object(requests, 'get') def test_response_verify(self): # make a call to the view using self.app.post (WebTest), # requests.get makes a suitable fake response from the mock object 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.