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" - see http://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 view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/Xd_8Uy6-X2QJ. 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.