I have just recently started working with Django as a part of my job. I am completely new to it, and I was working on a practice project while learning django.
I ask user to enter a phrase or a word in a search bar, and I return 50 results from twitter that have those words in the status message. The code works fine, and I have done some unit testing on it. Now I wanted to test the twitter API(Twython) that I am using, and I figured out I can use Mock or patch for doing it, but I am not able to understand how to go about it. I have read a couple of Documentations on Mocking but couldn't understand it very well.
Here is the snippet of my code
def search(request):
searches = []
query = ""
if request.method == "POST":
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
query = request.POST.get('q', '')
search_results = twitter.search(q=query, count=50)
for tweet in search_results['statuses']:
tweets = "Date : %s,\n Tweet : %s" % (tweet['created_at'], tweet['text'])
searches.append(tweets)
return render(request, "search.html", {
"results": searches,
"query1": query
})
Can someone please suggest how I can test this line using mock:
search_results = twitter.search(q=query, count=50)