Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion tests/strict_mock_testslide.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,9 @@ def raises_when_an_undefined_method_is_accessed(self):
UndefinedAttribute,
f"'{self.test_method_name}' is not set.\n"
f"{self.strict_mock_rgx} must have a value set "
"for this attribute if it is going to be accessed.",
"for this attribute if it is going to be accessed.\n"
r"Tip: use testslide.mock_callable\(\) to configure "
"this attribute.",
):
getattr(self.strict_mock, self.test_method_name)

Expand Down Expand Up @@ -863,6 +865,18 @@ async def raises_when_setting_a_non_callable_value(self):
):
setattr(self.strict_mock, self.method_name, "not callable")

@context.example
async def raises_when_an_undefined_method_is_accessed(self):
with self.assertRaisesWithRegexMessage(
UndefinedAttribute,
f"'{self.method_name}' is not set.\n"
"<StrictMock .+> must have a value set "
"for this attribute if it is going to be accessed.\n"
r"Tip: use testslide.mock_async_callable\(\) to configure "
"this attribute.",
):
getattr(self.strict_mock, self.method_name)

@context.sub_context
def signature_and_type_validation(context):
@context.shared_context
Expand Down
14 changes: 13 additions & 1 deletion testslide/core/strict_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,19 @@ def __setattr__(self, name: str, value: Any) -> None:

def __getattr__(self, name: str) -> Any:
if self._template and self.__template_has_attr(name):
raise UndefinedAttribute(self, name)
template_value = getattr(self._template, name, None)
if inspect.iscoroutinefunction(template_value):
message = (
"Tip: use testslide.mock_async_callable() to configure "
"this attribute."
)
elif callable(template_value):
message = (
"Tip: use testslide.mock_callable() to configure this attribute."
)
else:
message = None
raise UndefinedAttribute(self, name, message)
else:
raise AttributeError(f"'{name}' was not set for {repr(self)}.")

Expand Down