You don't need mocks or dependency injection in this case. Just separate the 
message construction code, so you can test it in isolation:

# myapp.views
from django.http import HttpResponse
from myapp.models import CurrentState
from myapp.exceptions import ApiFailureException
from third_party.api import theAPI 


def my_view(request):
    state = CurrentState.get(pk=request. session.get('state'))
    new_data = request.POST.get('new_data')
    state.update(new_data)
    msg = _get_message(state, new_data)
    result = theAPI.call(msg)
    if result is None:
        return ApiFailureException()
    return HttpResponse('success')

def _get_message(state, new_data):
    message = None
    if state.x() and state.y():
        message = '%s/%s' % (state.a(), new_data)
    elif state.z():
        message = '%s/%s' % (state.b(), new_data)
    else:
        message = state.c()
    return message


# third_party.api
import urllib, urllib2
from django.conf import settings

class theAPI(object):
    def call(self, msg=''):
        data = urllib.urlencode({'message': msg})
        req = urllib2.Request(url=settings.THIRD_PARTY_API_URL, data=data)
        resp = urllib2.urlopen(req)
        return resp.status == '200' 


-- 
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/-/sx939cizEBYJ.
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.

Reply via email to