簡體   English   中英

如何在Django測試框架中修改會話

[英]How do I modify the session in the Django test framework

我的網站允許個人在沒有登錄的情況下通過基於當前session_key創建用戶來貢獻內容

我想為我的視圖設置一個測試,但似乎無法修改request.session:

我想這樣做:

from django.contrib.sessions.models import Session
s = Session()
s.expire_date = '2010-12-05'
s.session_key = 'my_session_key'
s.save()
self.client.session = s
response = self.client.get('/myview/')

但我得到錯誤:

AttributeError: can't set attribute

關於如何在獲取請求之前修改客戶端會話的想法? 我已經看到了這個 ,它似乎並沒有工作

django測試框架的客戶端對象使觸摸會話成為可能。 請查看http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session了解詳情

注意: To modify the session and then save it, it must be stored in a variable first (because a new SessionStore is created every time this property is accessed)

我認為下面這樣的事情應該有效

s = self.client.session
s.update({
    "expire_date": '2010-12-05',
    "session_key": 'my_session_key',
})
s.save()
response = self.client.get('/myview/')

這就是我做到的方式(靈感來自http://blog.mediaonfire.com/?p=36中的解決方案)。

from django.test import TestCase
from django.conf import settings
from django.utils.importlib import import_module

class SessionTestCase(TestCase):
    def setUp(self):
        # http://code.djangoproject.com/ticket/10899
        settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file'
        engine = import_module(settings.SESSION_ENGINE)
        store = engine.SessionStore()
        store.save()
        self.session = store
        self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key

之后,您可以創建測試:

class BlahTestCase(SessionTestCase):

    def test_blah_with_session(self):
        session = self.session
        session['operator'] = 'Jimmy'
        session.save()

等等...

正如安德魯·奧斯汀已經提到的那樣,由於這個錯誤它不起作用: https//code.djangoproject.com/ticket/11475

你可以做的是:

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User

class SessionTestCase(TestCase):
    def setUp(self):
        self.client = Client()
        User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
        self.client.login(username='john', password='johnpassword')

    def test_something_with_sessions(self):
        session = self.client.session
        session['key'] = 'value'
        session.save()

使用User.objects.create_user()和self.client.login()創建並登錄用戶后,如上面的代碼所示,會話應該有效。

您可以創建插入虛擬數據(如會話)的自定義視圖。

具有相應url的視圖:/ dummy /:

def dummy(request):
    # dummy init data
    request.session['expiry_date'] = '2010-12-05'
    return HttpResponse('Dummy data has been set successfully')

比在測試腳本中只需調用self.client.get('/dummy/')

我還使用這個虛擬視圖在手動測試時初始化會話中的虛擬數據。

根據文檔,您可以通過例如向測試客戶端中的會話添加值

def test_something(self):
    session = self.client.session
    session['somekey'] = 'test'
    session.save()

https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.Client.session

這將允許您測試需要會話中的數據才能正常運行的視圖。

所以對於這個問題:

session = self.client.session
   session['expire_date'] = '2010-12-05'
   .....
   session.save()

您可以使用中間件修改會話。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM