mockify.cardinality - Classes for setting expected call cardinality¶
Module containing set of classes to be used with
mockify.engine.Expectation.times()
method.
You can also create your own classes to be used with that method. The only thing required from such class is to implement following interface:
is_satisfied(self, actual)
Return
True
ifactual
call count is satisfied byself
, orFalse
otherwise.Here,
actual
is absolute call count expectation received so far. It is completely implementation-specific of which values ofactual
are said to be satisfied and which are not. For example,Exactly
will compareactual
with fixed value (given via constructor) and returnTrue
only if those two are equal.adjust_by(self, minimal)
Adjust
self
by currentminimal
expected call count and return new instance oftype(self)
.In some complex expectation there could be a situation in which expectation must be computed again. This is not visible for library user, but must be done behind the scenes to properly process expectations. Such situation can be presented in this example:
>>> from mockify.actions import Return >>> from mockify.mock import Function >>> foo = Function('foo') >>> foo.expect_call(1, 2).will_once(Return(1)).will_repeatedly(Return(2)).times(2) <mockify.Expectation: foo(1, 2)> >>> foo(1, 2) 1 >>> foo(1, 2) 2 >>> foo(1, 2) 2 >>> foo.assert_satisfied()In example above we’ve used
times(2)
to tell that last repeated action is expected to be called twice, but real expected call count is 3 times, aswill_once
is used. Behind the scenes, this is recalculated using this metho.format_expected(self)
Return textual representation of expected call count.
This is used by
mockify.exc.Unsatisfied
exception when error message is being rendered.minimal(sefl)
(property)Property containing minimal call count that is considered valid for given instance.
For example, for
AtLeast
orExactly
it would be just its constructor argument, for :class`AtMost` it will be 0, forBetween
it will be itsminimal
argument.
-
class
mockify.cardinality.
AtLeast
(minimal)¶ Bases:
object
Used to set minimal expected call count.
If this is used, then expectation is said to be satisfied if actual call count is not less that
minimal
.Parameters: minimal – Integer value representing minimal expected call count
-
class
mockify.cardinality.
AtMost
(maximal)¶ Bases:
object
Used to set maximal expected call count.
If this is used, then expectation is said to be satisfied if actual call count is not greater than
maximal
.Parameters: maximal – Integer value representing maximal expected call count
-
class
mockify.cardinality.
Between
(minimal, maximal)¶ Bases:
object
Used to set a range of valid call counts.
If this is used, then expectation is said to be satisfied if actual call count is not less than
minimal
and not greater thanmaximal
.Parameters: - minimal – Integer value representing minimal expected call count
- maximal – Integer value representing maximal expected call count
-
class
mockify.cardinality.
Exactly
(expected)¶ Bases:
object
Used to expect fixed call count to be made.
You do not have to use this class explicitly as its instances are automatically created when you call
times
method with integer value as argument.Parameters: expected – Integer value representing expected call count