February 2022
Intermediate to advanced
274 pages
6h 28m
English
Now let’s check to make sure the Cards CLI deals with error conditions correctly. For example, here’s the delete command implementation:
| | @app.command() |
| | def delete(card_id: int): |
| | """Remove card in db with given id.""" |
| | with cards_db() as db: |
| | try: |
| | db.delete_card(card_id) |
| | except cards.InvalidCardId: |
| | print(f"Error: Invalid card id {card_id}") |
To test the CLI’s handling of an error condition, we can pretend that delete_card generates an exception by assigning the exception to the mock object side_effect attribute, like this:
| | def test_delete_invalid(mock_cardsdb): |
| | mock_cardsdb.delete_card.side_effect = cards.api.InvalidCardId |
| | out = cards_cli( ... |
Read now
Unlock full access