Opened 14 years ago
Closed 13 years ago
#15826 closed Bug (fixed)
TestCase assertions should work with TemplateResponse
Reported by: | bmihelac | Owned by: | mmcnickle |
---|---|---|---|
Component: | Testing framework | Version: | 1.3 |
Severity: | Normal | Keywords: | |
Cc: | mmcnickle | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When using assertContains for testing class based generic views or other views that returns TemplateResponse, following error is raised:
ContentNotRenderedError: The response content must be rendered before it can be accessed.
Example:
response = SomeView.as_view()(request) self.assertContains(response, 'some content')
This can be avoided with adding response.render() before assertion is called, but in my opinion assertContain should check if content is rendered instead.
Attachments (2)
Change History (7)
comment:1 by , 14 years ago
Easy pickings: | unset |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 14 years ago
Cc: | added |
---|---|
Has patch: | set |
Owner: | changed from | to
Status: | new → assigned |
by , 14 years ago
Attachment: | 15826.diff added |
---|
follow-up: 4 comment:3 by , 14 years ago
I added a check if the response handed to assertContains is a TemplateResponse, and if so, will render it.
Added the same check to assertNotContains for consistency.
I do an explicit check for instanceof(response, SimpleTemplateResponse), but perhaps duck typing for any response with response.render() would be more appropriate/useful?
comment:4 by , 14 years ago
Replying to mmcnickle:
I do an explicit check for instanceof(response, SimpleTemplateResponse), but perhaps duck typing for any response with response.render() would be more appropriate/useful?
Yes, I think it'd be better to check for a callable response.render() rather than using isinstance. This (checking for the render method) is what Django already does in its normal response handling, after template response middleware.
comment:5 by , 13 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
In [17025]:
(The changeset message doesn't reference this ticket)
Render TemplateReponse inside assertContains