Skip to content

Commit 5811563

Browse files
committed
Mock the Field class, not just its init method
Mocking the init method is messy because then the code in the real `__init__` method doesn’t get run, so properties like `content` don’t get set. We can still test the same thing (which arguments the `Field` is instantiated with) by mocking the whole class. Then any downstream code which is looking at the field will be dealing with a mock, which returns something for properties like `content` which don’t exist.
1 parent f238dd9 commit 5811563

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

tests/test_template_types.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,8 +1312,7 @@ def test_is_message_empty_email_and_letter_templates_tries_not_to_count_chars(
13121312
"sms",
13131313
{},
13141314
[
1315-
mock.call("content"), # This is to get the placeholders
1316-
mock.call("content", {}, html="passthrough"),
1315+
mock.call("content"),
13171316
],
13181317
),
13191318
(
@@ -1392,11 +1391,9 @@ def test_is_message_empty_email_and_letter_templates_tries_not_to_count_chars(
13921391
),
13931392
],
13941393
)
1395-
@mock.patch("notifications_utils.template.Field.__init__", return_value=None)
1396-
@mock.patch("notifications_utils.template.Field.__str__", return_value="1\n2\n3\n4\n5\n6\n7\n8")
1394+
@mock.patch("notifications_utils.template.Field")
13971395
def test_templates_handle_html_and_redacting(
1398-
mock_field_str,
1399-
mock_field_init,
1396+
mock_field,
14001397
template_class,
14011398
template_type,
14021399
extra_args,
@@ -1405,7 +1402,7 @@ def test_templates_handle_html_and_redacting(
14051402
assert str(
14061403
template_class({"content": "content", "subject": "subject", "template_type": template_type}, **extra_args)
14071404
)
1408-
assert mock_field_init.call_args_list == expected_field_calls
1405+
assert mock_field.call_args_list == expected_field_calls
14091406

14101407

14111408
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)