Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def it_doest_not_type_validate(self):
call_args = [1 for arg in self.call_args]
call_kwargs = {key: 1 for key in self.call_kwargs.keys()}
mock_callable(
self.target_arg,
self.callable_arg,
type_validation=self.type_validation,
).for_call(*call_args, **call_kwargs).to_return_value(None)
self.callable_target(*call_args, **call_kwargs)
def mock_callable_dsl(self):
return mock_callable(
self.target_arg,
self.callable_arg,
type_validation=self.type_validation,
)
def mock_callable_dsl(self):
return mock_callable(
self.target_arg,
self.callable_arg,
type_validation=self.type_validation,
)
def it_is_not_allowed(self):
with self.assertRaises(ValueError):
mock_callable(sample_module.Target, "__str__")
def mock_callable_dsl(self):
return mock_callable(
self.target_arg,
self.callable_arg,
type_validation=self.type_validation,
)
def newest_mock_has_precedence_over_older_mocks(self):
"""
Mocks are designed to be composable, allowing us to declare
multiple behaviors for different calls. Those definitions stack up,
and when a call is made to the mock, they are searched from newest
to oldest, to find one that is able to be caled.
"""
# First, mock all calls
mock_callable(self.target_arg, self.callable_arg).to_return_value(
"any args"
)
# Then we add some specific call behavior
mock_callable(self.target_arg, self.callable_arg).for_call(
*self.specific_call_args, **self.specific_call_kwargs
).to_return_value("specific")
# The first behavior should still be there
self.assertEqual(
self.callable_target(*self.call_args, **self.call_kwargs),
"any args",
)
# as well as the specific case
self.assertEqual(
self.callable_target(
*self.specific_call_args, **self.specific_call_kwargs
),
def other_instances_are_not_mocked(self):
mock_callable(self.target_arg, self.callable_arg).to_return_value(
"mocked value"
)
self.assertEqual(
self.callable_target(*self.call_args, **self.call_kwargs),
"mocked value",
)
other_strict_mock = StrictMock(
template=sample_module.Target, runtime_attrs=runtime_attrs
)
mock_callable(other_strict_mock, self.callable_arg).to_return_value(
"other mocked value"
)
self.assertEqual(
getattr(other_strict_mock, self.callable_arg)(
*self.call_args, **self.call_kwargs
),
"other mocked value",
)
def mock_callable_dsl(self):
return mock_callable(self.target_arg, self.callable_arg)