-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
8 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,19 @@ | ||
# Goal | ||
|
||
Unit test the printCurrentDate function. | ||
|
||
1. Test coupled code | ||
2. Test the code with doubles from a library. | ||
3Test the code with doubles created by you. | ||
3. Test the code with doubles created by you. | ||
|
||
# Code to test | ||
```python | ||
class PrintDate: | ||
def print_current_date(self): | ||
print(datetime.today()) | ||
# Learnings | ||
How to build a Mock and Stub manually. | ||
|
||
How to use Unittest Mock to generate the doubles. | ||
|
||
## Tools | ||
[Unittest documentation](https://cpython-test-docs.readthedocs.io/en/latest/library/unittest.mock.html) | ||
### Example of spy | ||
``` | ||
|
||
def test_should_send_an_email(self): | ||
emailSender = Mock(EmailSender) | ||
user_registration = UserRegistration(email_sender) | ||
|
||
user_registration.register() | ||
|
||
email_sender.send.assert_called() | ||
|
||
|
||
### Example of stub | ||
|
||
def test_should_success_when_password_is_valid(self): | ||
password_validator = Mock(PasswordValidator) | ||
password_validator.is_valid = Mock(return_value=true) | ||
user_registration = UserRegistration(password_validator) | ||
|
||
success = user_registration.register() | ||
# Learnings | ||
|
||
assertTrue(success) | ||
- How to use Stubs and Spies to generate the doubles. | ||
- How to build a Mock and Stub manually. |