Mocking
When it comes to unit testing, you always want to test a unit of your piece of code. Your code might depends on third party services such as an API and you don't want to call it during your local tests or in your CI environment. That's when you should use mocking to mock the external parts or the part you don't want to test.
Masonite comes with some mocking abilities for some of the features relying on third party services. For other parts or you own code you can use Python mocking abilities provided by unittest.mock standard module.

Masonite Features Mocks

Masonite tests case have two helpers method fake() and restore().
You can mock a Masonite feature by doing self.fake(feature) and then restore it to the real feature behaviour by calling self.restore(feature). When a feature is mocked the real behaviour won't be called, instead a quick and simple implementation is ran, often offering the ability to inspect and test what happens.
Available features that can be mocked (for now) are:

Mocking Mail

When mocking emails it will prevent emails from being really sent. Typically, sending mail is unrelated to the code you are actually testing. Most likely, it is sufficient to simply assert that Masonite was instructed to send a given mailable.
Here is an example of how to mock emails sending in your tests:
1
def setUp(self):
2
super().setUp()
3
self.fake("mail")
4
5
def tearDown(self):
6
super().tearDown()
7
self.restore("mail")
8
9
def test_mock_mail(self):
10
welcome_email = self.application.make("mail").mailable(Welcome()).send()
11
(
12
welcome_email.seeEmailContains("Hello from Masonite!")
13
.seeEmailFrom("[email protected]")
14
.seeEmailCountEquals(1)
15
)
Copied!
Available assertions are:
  • seeEmailWasSent()
  • seeEmailWasNotSent()
  • seeEmailCountEquals(count)
  • seeEmailTo(string)
  • seeEmailFrom(string)
  • seeEmailReplyTo(string)
  • seeEmailBcc(string)
  • seeEmailCc(string)
  • seeEmailSubjectEquals(string)
  • seeEmailSubjectContains(string)
  • seeEmailSubjectDoesNotContain(string)
  • seeEmailContains(string)
  • seeEmailDoesNotContain(string)
  • seeEmailPriority(string)

Mocking Notification

When mocking notifications it will prevent notifications from being really sent. Typically, sending notification is unrelated to the code you are actually testing. Most likely, it is sufficient to simply assert that Masonite was instructed to send a given notification.
Here is an example of how to mock notifications sending in your tests:
1
def setUp(self):
2
super().setUp()
3
self.fake("notifications")
4
5
def tearDown(self):
6
super().tearDown()
7
self.restore("notifications")
8
9
def test_mock_notification(self):
10
notification = self.application.make("notification")
11
notification.assertNothingSent()
12
notification.route("mail", "[email protected]").send(WelcomeNotification())
13
notification.route("mail", "[email protected]").send(WelcomeNotification())
14
notification.assertCount(2)
Copied!
1
def test_mock_notification(self):
2
self.application.make("notification").route("mail", "[email protected]").route(
3
"slack", "#general"
4
).send(OrderNotification(10))
5
self.application.make("notification").assertLast(
6
lambda user, notif: (
7
notif.assertSentVia("mail")
8
.assertEqual(notif.order_id, 10)
9
.assertEqual(
10
notif.to_slack(user).get_options().get("text"),
11
"Order 10 has been shipped !",
12
)
13
)
14
)
Copied!
Available assertions are:
  • assertNothingSent()
  • assertCount(count)
  • assertSentTo(notifiable, notification_class, callable_assert=None, count=None)
  • assertLast(callable_assert)
  • assertNotSentTo(notifiable, notification_class)
On Notifications instances:
  • assertSentVia(*drivers)
  • assertEqual(value, reference)
  • assertNotEqual(value, reference)
  • assertIn(value, container)
Available helpers are:
  • resetCount()
  • last()

Basic Python mocks

For mocking any piece of code in Python you can use the standard unittest.mock module. You can find more information in unittest documentation.
Here is basic example
1
from unittest.mock import patch
2
3
with patch("some.module") as SomeClass:
4
SomeClass.return_value.my_method.return_value = 0
5
self.assertEqual(SomeClass().my_method(), 0)
Copied!
For mocking external HTTP requests you can use the responses module. You can find more information in responses documentation.
1
import responses
2
3
@responses.activate
4
def test_mock_third_party_api(self):
5
responses.add(responses.POST, "api.github.com", body=b"Ok")
6
# do something in your code
7
self.assertTrue(responses.assert_call_count("api.github.com", 1))
Copied!