Skip to content

expect's count checking error message is inconsistent when multiple thenReturn clauses are provided. #125

@Kyle-sandeman-mrdfood

Description

@Kyle-sandeman-mrdfood

Simple example:

def test():
  with expect(xyz, times=1).abc().thenReturn(1, 2):
    func_that_uses_abc_once()

This produces different, seemingly contradictory error messages.

  • times=0 or between=(0,0) results in e.g. "Wanted between: [0, 0], actual times: 1"
  • times=1 or between=(1,1) results in "Unused stub: abc()"
  • times=2 or between=(2,0) results in e.g. "Wanted between: [2, inf], actual times: 1"

After all, how can times=0 say it was executed once, but times=1 says it is unused?

Just to be clear, I am fully aware that thenReturn(1, 2) is the issue 😄 but the error message led me down a rabbit hole around times=1 rather than around thenReturn

Given the len(answers) part in the mockito codebase, maybe a clearer message can be produced if times (and the others) do not match the answer count?
I imagine due to all the chaining that one can do (e.g. .thenReturn(x).thenRaise(y).thenReturn(a, b)) this isn't an easy problem to solve, but if the check_used error message is more clear then one can self-diagnose very quickly.

Maybe something like
Unused stub: .abc(), which also has unused answers?

Once again thank you for the wonderful library!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions