Commits

Anonymous committed 9c22d9f

assert_context

Comments (0)

Files changed (2)

flaskext/testing.py

 
 __all__ = ["TestCase", "TwillTestCase"]
 
+class ContextVariableDoesNotExist(Exception):
+    pass
+
 class JsonResponseMixin(object):
     """
     Mixin with testing helper methods
         template. Only works if your version of Flask
         has signals support (0.6+) and blinker is installed.
 
+        Raises a ContextVariableDoesNotExist exception if does
+        not exist in context.
+
         :param name: name of variable
         """
         assert _is_signals, "Signals not supported"
         for template, context in self.templates:
             if name in context:
                 return context[name]
+        raise ContextVariableDoesNotExist
+
+    def assertContext(self, name, value):
+        """
+        Checks if given name exists in the template context
+        and equals the given value.
+
+        :param name:name of context variable
+        :param value: value to check against
+        """
+
+        try:
+            assert self.get_context_variable(name) == value
+        except ContextVariableDoesNotExist:
+            assert False
+
+    assert_context = assertContext
 
     def assertRedirects(self, response, location):
         """

tests/__init__.py

 from flask import Flask, Response, abort, redirect, url_for, \
     jsonify, render_template
 
-from flaskext.testing import TestCase, TwillTestCase
+from flaskext.testing import TestCase, TwillTestCase, \
+    ContextVariableDoesNotExist
 
 def create_app():
 
 
         response = self.client.get("/template/")
         assert self.get_context_variable("name") == "test"
+
+    def test_assert_context(self):
+
+        response = self.client.get("/template/")
+        self.assert_context("name", "test")
+
+    def test_assert_bad_context(self):
+
+        response = self.client.get("/template/")
+        self.assertRaises(AssertionError, self.assert_context, "name", "foo")
+        self.assertRaises(AssertionError, self.assert_context, "foo", "foo")
+
+    def test_assert_get_context_variable_not_exists(self):
+
+        response = self.client.get("/template/")
+        self.assertRaises(ContextVariableDoesNotExist, 
+                          self.get_context_variable, "foo")
+