summaryrefslogtreecommitdiff
path: root/repoze/bfg/testing.py
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2009-01-07 04:15:27 +0000
committerChris McDonough <chrism@agendaless.com>2009-01-07 04:15:27 +0000
commit7d32df3d33354dc34c1cb0a086463e4f23d968ce (patch)
treedc33728f73b23a35111eb2d6a5a0077426320266 /repoze/bfg/testing.py
parentd67c741ef82c0a14778c691281139ec364c34a27 (diff)
downloadpyramid-7d32df3d33354dc34c1cb0a086463e4f23d968ce.tar.gz
pyramid-7d32df3d33354dc34c1cb0a086463e4f23d968ce.tar.bz2
pyramid-7d32df3d33354dc34c1cb0a086463e4f23d968ce.zip
- Add a method named ``assert_`` to the DummyTemplateRenderer. This
method accepts keyword arguments. Each key/value pair in the keyword arguments causes an assertion to be made that the renderer received this key with a value equal to the asserted value. - Projects generated by the paster templates now use the ``DummyTemplateRenderer.assert_`` method in their view tests.
Diffstat (limited to 'repoze/bfg/testing.py')
-rw-r--r--repoze/bfg/testing.py41
1 files changed, 39 insertions, 2 deletions
diff --git a/repoze/bfg/testing.py b/repoze/bfg/testing.py
index 94f798469..be7b5fea0 100644
--- a/repoze/bfg/testing.py
+++ b/repoze/bfg/testing.py
@@ -5,7 +5,7 @@ from zope.interface import implements
from repoze.bfg.interfaces import IRequest
-_marker = ()
+_marker = []
def registerDummySecurityPolicy(userid=None, groupids=(), permissive=True):
""" Registers a dummy ``repoze.bfg`` security policy using the
@@ -172,13 +172,50 @@ def make_traverser_factory(root):
return DummyTraverserFactory
class DummyTemplateRenderer:
+ """
+ An instance of this class is returned from
+ ``registerDummyRenderer``. It has a helper function (``assert_``)
+ that makes it possible to make an assertion which compares data
+ passed to the renderer by the view function against expected
+ key/value pairs.
+ """
+
+ def __init__(self):
+ self._received = {}
+
def implementation(self):
return self
def __call__(self, **kw):
- self.__dict__.update(kw)
+ self._received.update(kw)
return ''
+ def __getattr__(self, k):
+ """ Backwards compatibiity """
+ val = self._received.get(k, _marker)
+ if val is _marker:
+ raise AttributeError(k)
+ return val
+
+ def assert_(self, **kw):
+ """ Accept an arbitrary set of assertion key/value pairs. For
+ each assertion key/value pair assert that the renderer
+ (eg. ``render_template_to_response``) received the key with a
+ value that equals the asserted value. If the renderer did not
+ receive the key at all, or the value received by the renderer
+ doesn't match the assertion value, raise an AssertionError."""
+ for k, v in kw.items():
+ myval = self._received.get(k, _marker)
+ if myval is _marker:
+ raise AssertionError(
+ 'A value for key "%s" was not passed to the renderer' % k)
+
+ if myval != v:
+ raise AssertionError(
+ '\nasserted value for %s: %r\nactual value: %r' % (
+ v, k, myval))
+ return True
+
def make_view(result):
def dummy_view(context, request):
from webob import Response