All right, so you’ve heard about some of the common problems that come up when you use mocking, such as typos, misspellings, and interface changes. Fortunately, there is something called
spec in the
mock module that we can use to prevent some of these common problems. When you’re configuring a
Mock, you can pass a specification using the
This returns a
Mock object, which is good. That’s expected, that’s what we would want to happen. Let’s say that we want to try to access
.is_weekday() but we have a typo and we accidentally put an extra
Now this raises an
AttributeError, saying that the
Mock object has no attribute 'is_weekdayy', with two y’s. This is catching a common problem of misspelling the names of attributes or methods or object interfaces. So as a reminder, if we just set
calendar as a regular
Mock object without a
spec—so if we said
calendar = Mock(), and we said
calendar.is_weekday(), it would be a
Mock object. Cool.
then when we try to access the mistyped one, we get an
AttributeError. So this is super powerful. I highly recommend that you use
spec to define what your module actually serves, actually has available.
Become a Member to join the conversation.