Skip to content

Commit

Permalink
Mock test implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
dougfraga committed Sep 8, 2020
1 parent 137b631 commit 6acbe85
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions libpythonpro_doug/tests/test_spam/test_send_to_user_base.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
from unittest.mock import Mock

import pytest

from libpythonpro_doug.spam.email_sender import Sender
from libpythonpro_doug.spam.main import SpamSender
from libpythonpro_doug.spam.models import User


class SenderMock(Sender):

def __init__(self):
super().__init__()
self.num_of_sent_emails = 0
self.sent_params = None

def send(self, shipper, recipient, title, body):
self.sent_params = (shipper, recipient, title, body)
self.num_of_sent_emails += 1


@pytest.mark.parametrize(
'users',
[
Expand All @@ -32,28 +22,29 @@ def send(self, shipper, recipient, title, body):
def test_number_of_spam(session, users):
for user in users:
session.save(user)
sender = SenderMock()
sender = Mock()
spam_sender = SpamSender(session, sender)
spam_sender.send_emails(
'douglas.fraga@gmail.com',
'Python Pro Training Course',
'Email body'
)
assert len(users) == sender.num_of_sent_emails
assert len(users) == sender.send.call_count

def test_spam_params(session):
user = User(name='Doug', email='douglas.fraga@gmail.com')
session.save(user)
sender = SenderMock()
spam_sender = SpamSender(session, sender)
spam_sender.send_emails(
'caroldanelli@gmail.com',
'Python Pro Training Course',
'Email body'
)
assert sender.sent_params == (
'caroldanelli@gmail.com',
'douglas.fraga@gmail.com'
'Python Pro Training Course',
'Email body'
)

def test_spam_params(session):
user = User(name='Doug', email='douglas.fraga@gmail.com')
session.save(user)
sender = Mock()
spam_sender = SpamSender(session, sender)
spam_sender.send_emails(
'caroldanelli@gmail.com',
'Python Pro Training Course',
'Email body'
)
sender.send.assert_called_once_with(
'caroldanelli@gmail.com',
'douglas.fraga@gmail.com',
'Python Pro Training Course',
'Email body'
)

0 comments on commit 6acbe85

Please sign in to comment.