flask-testing icon indicating copy to clipboard operation
flask-testing copied to clipboard

'get_context_variable' sticks to first request.

Open serpulga opened this issue 11 years ago • 5 comments

I have a test case in which I do a couple requests using the test client; after every request I need to check the context and I use the get_context_variable method. The problem I'm experiencing is that the value returned by this function is always the expected value from the first requests, subsequent requests don't affect the value returned by this method.

serpulga avatar Jan 22 '14 21:01 serpulga

Sorry for my late response. Can you provide a tests case?

jarus avatar May 07 '14 11:05 jarus

@serpulga I had the same problem with my tests. In order to fix it I had overwritten the get_context_variable method for getting the correct template given a template name.

    def get_context_variable(self, name, template):
        """
        Returns a variable from the context passed to the
        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.
        """

      if not _is_signals:
          raise RuntimeError("Signals not supported")

      for templ, context in self.templates:
          if templ.name == template and name in context:
              return context[name]
      raise ContextVariableDoesNotExist

Should it be parte of the Flask-Testing core? If so, I can do a pull request with this change.

helielson avatar Jun 12 '14 03:06 helielson

@helielson This looks like a valid and working fix. If you could provide a pull request that would be fine. Thanks.

jarus avatar Jun 12 '14 05:06 jarus

Hey @jarus. I've changed the code above, but seems that the get_context_variable method always returns the context variable of the last render call. This change was done in this commit but I couldn't understand why. Do you know the reason of this self.templates reset? I removed this check and all tests work fine including my new test. If there is any reason for this reset let me know. Otherwise I'll remove it in my pull request.

helielson avatar Jun 12 '14 18:06 helielson

I just ran into this today as well - I have to do an initial request to log the "user" in, and then issue a request to the real request handler. It would be nice to get this fix incorporated so that I could test the variables in the real request.

btoconnor avatar Jul 10 '14 19:07 btoconnor