I'm not testing the third-party service. I need to test *what I send to them*. I.e. that the output of my_view is correct. The trouble is that neither my_view nor the API call actually returns the output that I need to check.
Does that make sense? On Jun 20, 1:59 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > On Monday, June 20, 2011 6:07:59 PM UTC+1, Nan wrote: > > > In most situations, my app, upon receiving an HTTP request, sends data > > to a third-party API, and returns an empty HttpResponse. I need to > > test that the correct data is sent to the third-party API based on > > internal application state. I'm perplexed as to how to intercept this > > data in a unit test. > > > Here's a sort of dummy version of the code in question. What I need > > to be able to test is that depending on the data in the CurrentState > > object, the value of the "message" parameter sent by the API call is > > correct. Any suggestions? > > Don't test the third-party service. You don't have any control over it - you > just have to assume that it works, and always does what it says in the > published API. > > The only bit you need to test is your interaction with that API. The best > way to do that is to use a technique called "mocking", where you replace the > part of your code which calls the API with a dummy object which always > returns what you tell it. Of course, there are plenty of libraries to enable > this, but my favourite is Michael Foord's one, simply called "mock" - > seehttp://www.voidspace.org.uk/python/mock/ > > Using mock, you would patch your "theAPI" class to use a mock function in > place of the "call" method, which returns the right data depending on the > parameters. Then assert that your code does the correct things in response. > -- > DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.