In this recipe, we will take a look at how to write unit tests. Unit tests are those that check whether individual functions or methods return the correct results. We will look at the likes app and write tests that check whether posting to the json_set_like() view returns a failure response for unauthenticated users and a successful result for authenticated users. We will use Mock objects to simulate the HttpRequest and AnonymousUser objects.
Testing views with mock
Getting ready
Let's start with the locations and likes apps from the Implementing the Like widget recipe in Chapter 4, Templates and JavaScript.
We are going to use the mock library, which has been available as a built-in at unittest.mock since Python 3.3...