I have a python (django) web application. It uses an external web service (Facebook Graph). All the code for making external (http) calls is wrapped in one extra function (called facebook_api), which takes some arguments and returns a parsed dict (it does some logging, checks for errors etc.) Around this function, I have written some code. I want to do some unittests of this code.
How can I “mock” this one function with some known input? i.e. I want to provide some input & output for my facebook_api function (i.e. if you see this input, return this, and if it’s this, return that, etc.). I then want to check that my code handles it OK.
I want to provide various different scenarios for my code (what if the API started returning this, or that) and check that my code does what I want. I don’t want to actually make external (http) requests.
I tried looking at some mock libraries (e.g.http://www.voidspace.org.uk/python/mock/ ), but they always talk about creating class objects, or just checking that something was called. I can’t see easy ways to say “Intercept this function, and if the input is A, return X, if it’s like D, return B”
I think you are looking for Mock’s side_effect . For example