Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of mocks for causes in the tests of registries #521

Merged
merged 1 commit into from
Aug 29, 2020

Conversation

nolar
Copy link
Owner

@nolar nolar commented Aug 29, 2020

What do these changes do?

Refactor tests that previously used mocks for causes — to use proper causes' classes. Mocks are bad.

Description

Causes need to be checked with isinstance() in the code base, to properly pass the type checking: isinstance() adjusts the inferred type for operations that follow the check. An example is handler-matching functions that check for causes to be resource-changing causes, and using .diff only after that (.diff is absent in all other causes).

However, this does not work with mocks in tests: all tests do fail, because Mock and MagicMock classes do not pass the isinstance() criterion.

Besides that, mocks are just a terrible way of testing: first of all, they leak the abstractions of the mocked objects by knowing which fields they contain, which properties they implement and how (and can mimic them wrong); second, they prevent tests to be type-checked both by type-checkers or by IDEs.

This PR improves the test design for registries & causes so that the following PRs can also rely on proper typing of arguments (e.g. #518).

Issues/PRs

Issues: #338

Related: #518 #375

Type of changes

  • Refactoring (non-breaking change which does not alter the behaviour)

Checklist

  • The code addresses only the mentioned problem, and this problem only
  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt

@nolar nolar added the refactoring Code cleanup without new features added label Aug 29, 2020
Causes need to be checked with `isinstance()` in the code base, to properly
pass the type checking: `isinstance()` adjusts the inferred type for operations
that follow the check. An example is handler-matching functions that check for
causes to be resource-changing causes, and using `.diff` only after that
(`.diff` is absent in all other causes).

However, this does not work with mocks in tests: all tests do fail, because
`Mock` and `MagicMock` classes do not pass the `isinstance()` criterion.

Besides that, mocks are just a terrible way of testing: first of all, they leak
the abstractions of the mocked objects by knowing which fields they contain,
which properties they implement and how (and can mimic them wrong); second,
they prevent tests to be type-checked both by type-checkers or by IDEs.
@nolar nolar force-pushed the cause-factory-in-tests branch from 0795e49 to 91875a4 Compare August 29, 2020 19:09
@nolar nolar merged commit 49b342b into master Aug 29, 2020
@nolar nolar deleted the cause-factory-in-tests branch August 29, 2020 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring Code cleanup without new features added
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant