Django:在@login_required的视图中测试失败 [英] Django: test failing on a view with @login_required

查看:430
本文介绍了Django:在@login_required的视图中测试失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为
@login_required装饰的视图建立一个测试,因为我没有使其工作,我做了一个简单的测试
,仍然无法通过。

I'm trying to build a test for a view that's decorated with @login_required, since I failed to make it work, I did a simple test and still can't make it pass.

以下是简单测试和视图的代码:

Here is the code for the simple test and the view:

def test_login(self):
    user = self._create_new_user()
    self.assertTrue(user.is_active)
    login = self.client.login(username=user.username,
password=self.data['password1'])
    self.failUnless(login, 'Could not log in')
    response = self.client.get('/accounts/testlogin/')
    self.assertEqual(response.status_code, 200)

@login_required
def testlogin(request):
    print 'testlogin !! '
    return HttpResponse('OK')

_create_new_user()正在保存用户是
方法中的一个测试,以确定它正在工作。

_create_new_user() is saving the user and there is a test inside that method to see that is working.

在response.status_code中,测试失败,返回302和
响应实例是一个HttpResponseRedirect,如果没有登录,将其重定向为

The test fails in the response.status_code, returning 302 and the response instance is of a HttpResponseRedirect, is redirecting it as if not logged in.

任何线索?我错过了一些东西?

Any clue? I'm missing something?

Regards
Esteban

Regards Esteban

推荐答案

p>这是答案:


Python 2.6.5更改了
cookie的存储方式,这是微妙的
与测试客户端不兼容。
这个问题已经在
1.1.X和trunk分支中修复了,但是修复还没有使它成为正式的
版本。

Python 2.6.5 made a change to the way cookies are stored which is subtly incompatible with the test client. This problem has been fixed in the 1.1.X and trunk branches, but the fix hasn't yet made it into a formal release.

如果您使用1.1.X和Python
2.6.5,那么您将遇到涉及
Cookie的任何测试活动出现问题。您需要降级
Python,或使用1.1.X分支,而不是1.1.1版本的

If you are using 1.1.X and Python 2.6.5, you're going to have problems with any test activity involving cookies. You either need to downgrade Python, or use the 1.1.X branch rather than the 1.1.1 release.

A 1.1.2版本(这将包括您描述的问题的
修复)将在
同时发布
发布1.2 - 希望非常非常非常

A 1.1.2 release (that will include the fix for the problem you describe) will be made at the same time that we release 1.2 - hopefully, very very soon.

Yours,Russ Magee% - )

Yours, Russ Magee %-)

http://groups.google .com / group / django-users / browse_frm / thread / 617457f5d62366ae / 05f0c01fff0b9e6d?hl = en& lnk = gst& q = 2.6.5#05f0c01fff0b9e6d

这篇关于Django:在@login_required的视图中测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆