File tree Expand file tree Collapse file tree 2 files changed +39
-3
lines changed
Expand file tree Collapse file tree 2 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -715,9 +715,23 @@ def verify(self) -> None:
715715 self .verification .verify (self , self .used )
716716
717717 def check_used (self ) -> None :
718- if not self .allow_zero_invocations and self .used < len (self .answers ):
718+ if self .allow_zero_invocations :
719+ return
720+
721+ expected_uses = len (self .answers )
722+ if self .used >= expected_uses :
723+ return
724+
725+ if self .used == 0 :
719726 raise verificationModule .VerificationError (
720- "\n Unused stub: %s" % self )
727+ "\n Unused stub: %s" % self
728+ )
729+ else :
730+ raise verificationModule .VerificationError (
731+ "\n Only %s of %s answers were used for %s"
732+ % (self .used , expected_uses , self )
733+ )
734+
721735
722736class StubbedPropertyAccess (StubbedInvocation ):
723737 def ensure_mocked_object_has_attribute (self , method_name : str ) -> None :
Original file line number Diff line number Diff line change @@ -362,9 +362,31 @@ def testFailSecondAnswerUnused(self):
362362 when (Dog ).bark ('Miau' ).thenReturn ('Yep' ).thenReturn ('Nop' )
363363 rex = Dog ()
364364 rex .bark ('Miau' )
365- with pytest .raises (VerificationError ):
365+ with pytest .raises (VerificationError ) as exc :
366366 verifyStubbedInvocationsAreUsed (Dog )
367367
368+ assert str (exc .value ) == (
369+ "\n Only 1 of 2 answers were used for bark('Miau')"
370+ )
371+
372+ def testFailOnlyTwoOfThreeAnswersUsed (self ):
373+ (
374+ when (Dog )
375+ .bark ('Miau' )
376+ .thenReturn ('Yep' )
377+ .thenReturn ('Nop' )
378+ .thenReturn ('Nope' )
379+ )
380+ rex = Dog ()
381+ rex .bark ('Miau' )
382+ rex .bark ('Miau' )
383+ with pytest .raises (VerificationError ) as exc :
384+ verifyStubbedInvocationsAreUsed (Dog )
385+
386+ assert str (exc .value ) == (
387+ "\n Only 2 of 3 answers were used for bark('Miau')"
388+ )
389+
368390
369391@pytest .mark .usefixtures ('unstub' )
370392class TestImplicitVerificationsUsingExpect :
You can’t perform that action at this time.
0 commit comments