Skip to content

Unexpected interaction with mocks #1139

@bbc2

Description

@bbc2

The bug only affects 2.11.0. It doesn't occur on 2.10.3.

from unittest import mock

import jinja2

foo = mock.Mock()
foo.return_value = "bar"
template = jinja2.Template("{{ foo() }}")
print(template.render(foo=foo))
foo.assert_called_with()

Expected Behavior

> python foo.py
bar

Actual Behavior

> python foo.py
bar
Traceback (most recent call last):
  File "foo.py", line 9, in <module>
    foo.assert_called_with()
  File "/usr/lib/python3.8/unittest/mock.py", line 907, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: expected call not found.
Expected: mock()
Actual: mock(<Context {'range': <class 'range'>, 'dict': <class 'dict'>, 'lipsum': <function generate_lorem_ipsum at 0x7f8db9caa940>, 'cycler': <class 'jinja2.utils.Cycler'>, 'joiner': <class 'jinja2.utils.Joiner'>, 'namespace': <class 'jinja2.utils.Namespace'>, 'foo': <Mock id='140246696713376'>} of None>)

Your Environment

  • Python version: 3.8.1
  • Jinja version: 2.11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions