开发者

django testing response.context TypeError: 'NoneType' object is not subscriptable

开发者 https://www.devze.com 2023-04-01 13:50 出处:网络
I want to test this view: def register(request): \"\"\" handle user registration code variable is for testing purposes

I want to test this view:

def register(request):
    """
    handle user registration
    code variable is for testing purposes
    """
    if request.method== 'GET':
        form = RegisterForm(auto_id=False)
        code = 1
        return render_to_response('register_home.html',locals(),context_instance=RequestContext(request))
    elif request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            password = form.cleaned_data['password']
            password_confirmation = form.cleaned_data['password_confirmation']
            if password == password_confirmation:
            #if True:
                login = form.cleaned_data['login']
                email = form.cleaned_data['email']
                newsletter = form.cleaned_data['newsletter']
                key = register_user(login,email,password,newsletter)
                if key:             
                    #send email
                    send_mail("Dziękujemy za rejestrację"," Klucz aktywacyjny to " + key,settings.EMAIL_HOST_USER,[email])
                    request.session['email'] = email
                    return redirect(regi开发者_开发百科ster_success)
                else:
                    code = 4
                    error = "Login /email are taken"
                    return render_to_response('register_home.html',locals(),context_instance=RequestContext(request))
            else:
                code = 3
                error  = "invalid password"
                return render_to_response('register_home.html',locals(),context_instance=RequestContext(request))
        else:
            code = 2
            return render_to_response('register_home.html',locals(),context_instance=RequestContext(request))

And here is my part of my test:

def test_valid_credentials(self):
        #now try to register valid user
        data = {'login':'test','password':'zaq12wsx','password_confirmation':'zaq12wsx','terms':True,'newsletter':True,'email':'test@test.com'}
        response = self.c.post(reverse('register'),data)
        #our user should be registred
        self.assertEquals(302, response.status_code,'We dont have benn redirected')
        self.assertEqual(len(mail.outbox), 1,'No activation email was sent')
        #clen email box
        mail.outbox = []
        #now try to add anotheer user with the same data
        response = self.c.post(reverse('register'),data)
        #template should be rendered with error message about used login and email
        self.assertEqual(response.context['code'],4)

And here is the error that I got: ,

in test_valid_credentials
    self.assertEqual(response.context['code'],4)
TypeError: 'NoneType' object is not subscriptable

I tried it with get method and it works perfectly. Just with post it don't want to work.What am I doing wrong?Best regards


What is the response status? Redirects doesn't have context. Anyway, printing the response should help.


My guess is

key = register_user(login,email,password,newsletter)

throws an exception on duplicate register attempts and thus the handler does not generate a response.

0

精彩评论

暂无评论...
验证码 换一张
取 消