Skip to content

Conversation

@samuelint
Copy link
Contributor

When using decoy.when across multiple test cases, you may want the mock to return a result regardless of the argument values—even if some are None. To handle all combinations, you'd typically need to define multiple matchers like:

decoy.when(
    mock_instance.execute(
        arg1=matchers.Anything(),
        arg2=None,
    )
).then_return(self.some_result)
decoy.when(
    mock_instance.execute(
        arg1=matchers.Anything(),
        arg2=matchers.Anything(),
    )
).then_return(self.some_result)
decoy.when(
    mock_instance.execute(
        arg1=None,
        arg2=matchers.Anything(),
    )
).then_return(self.some_result)

This can be simplified by using a matcher that accepts both None and any value:

decoy.when(
    mock_instance.execute(
        arg1=matchers.AnythingOrNone(),
        arg2=matchers.AnythingOrNone(),
    )
).then_return(self.some_result)

@codecov
Copy link

codecov bot commented Jun 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.42%. Comparing base (cb399b7) to head (0d184f9).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #262      +/-   ##
==========================================
+ Coverage   98.41%   98.42%   +0.01%     
==========================================
  Files          19       19              
  Lines         757      764       +7     
  Branches       93       93              
==========================================
+ Hits          745      752       +7     
  Misses         10       10              
  Partials        2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mcous mcous changed the title feat: AnythingOrNone matcher feat(matchers): add AnythingOrNone to match any value including None Jun 6, 2025
@mcous mcous merged commit 99af642 into mcous:main Jun 7, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants