#13092 closed (fixed)
ContextList objects do not support "in" operator
Reported by: | anonymous | Owned by: | nobody |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Keywords: | ContextList | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When running tests, if a single template is used to product a response to an HTTP request, then the test client's response context object is a regular Context (django.template.Context) object. When multiple templates are used, however, then the test client's response context object will be a django.test.utils.ContextList object instead.
The ContextList class tries to emulate a Context object for dictionary-style lookups, so that tests which reference response.contextkey will still work. However, ContextList does not implement _contains_, so expressions of the form "'key' in request.context", which would be true when only a single template is used, become false as soon as a second template is introduced.
For example, with this template:
<h1>No Content here</h1>
And this view:
from django.shortcuts import render_to_response def view(request): return render_to_response("test_template.html", { "foo": "bar" })
Then a test which says:
class SimpleTest(TestCase): def test_context(self): response = self.client.get("/test_url/") self.assertTrue("foo" in response.context)
will succeed, but if the template is changed to this:
{% extends "base.html" %} <h1>No content here</h1>
then the same test will fail. I'm attaching a patch which fixes this, as well as a two line patch for the Context/ContextList tests.
Attachments (1)
Change History (8)
by , 15 years ago
Attachment: | test_key_in_context_1.diff added |
---|
comment:1 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:5 by , 15 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:6 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch to allow tests like 'key in contextlist'