flask-testing
flask-testing copied to clipboard
'get_context_variable' sticks to first request.
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.
Sorry for my late response. Can you provide a tests case?
@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 This looks like a valid and working fix. If you could provide a pull request that would be fine. Thanks.
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.
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.